I've just started diving into AngularJS
and I'm facing some challenges with using angular.forEach
... Specifically, I have a data object retrieved from my REST API
...
My goal is to iterate through a nested array, look for a specific attribute, and if that attribute is set to true
, retrieve another attribute from the item.
Here's an example of the array structure;
orderItem: {
id: 159
name: Empanadas (Choice of 2)
description: Choice of Diced Beef; Spinach, Stilton and Onion; or Smoked Ham and Mozzarella
price: 700
available: 1
created_at: 2016-01-31 16:50:31
updated_at: 2016-01-31 16:50:31
menu_category_id: 41
restaurant_id: 11
menu_modifier_groups:
[ {
id: 9
name: Choose 2 Empanadas
instruction: null
min_selection_points: 2
max_selection_points: 2
force_selection: 1
created_at: 2016-02-01 01:03:35
updated_at: 2016-02-01 01:12:23
menu_item_id: 159
restaurant_id: 11
menu_modifier_items:
[ {
id: 34
name: Diced Beef
price: 0
created_at: 2016-02-01 01:04:08
updated_at: 2016-02-01 01:04:08
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: false
} , {
id: 35
name: Smoked Salmon & Mozzarella
price: 0
created_at: 2016-02-01 01:04:37
updated_at: 2016-02-01 01:04:37
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: true
} , {
id: 36
name: Stilton, Spinach and Onion
price: 0
created_at: 2016-02-01 01:05:05
updated_at: 2016-02-01 01:05:05
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: false
} ]
} ]
}
My objective is to identify all menu_modifier_items
that have selected
set to true
, retrieve their respective price
values, sum them up, and then add the total to the original price
of the orderItem
.
Sum of all menu_modifier_items prices + orderItem price
$scope.calculatePrice = function(orderItem) {
angular.forEach(orderItem, function(){
});
}
Any assistance or guidance would be greatly appreciated.