У меня есть объект, который я выборки от моего REST API
...AngularFor каждая - петля многократного массива
Я хочу проходной внутренний массив, проверить наличие атрибута и, если этот атрибут true
получить второй атрибут от предмета.
Это массив;
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
} ]
} ]
}
То, что я хочу сделать, это найти все menu_modifier_items
, которые имеют selected
= true
получить price
для каждого их сложить вместе, и, наконец, добавить их к price
из orderItem
.
all menu_modifier_items price + orderItem price
Это то, что я до сих пор;
$scope.calculatePrice = function(orderItem) {
var tot = orderItem.price;
angular.forEach(orderItem.menu_modifier_groups[0].menu_modifier_items,function(item){
if(item.selected == true) {
tot+=item.price;
}
return "Add for " + "₦" + tot;
});
}
В моем коде, menu_modifier_groups[0]
это означает, что я получаю первый массив, но что, если orderItem
имеет много menu_modifier_groups
? В случае, когда у меня есть menu_modifier_groups[0]
, menu_modifier_groups[1]
и т. Д.
Любая помощь/руководство оценено.