1

I have an object that I fetch from my REST API...

I want to loop through an inner array, check for an attribute and if that attribute is true get a second attribute from the item.

This is the array;

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
             }  ]
         }  ]
 }

What I want to do is find all menu_modifier_items that have selected = true get the price for each add them up together and finally add them to the price of orderItem.

all menu_modifier_items price + orderItem price

This is what I have so far;

$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;
 });
}

In my code, menu_modifier_groups[0] this means that I'm getting the first array, but what if orderItem has many menu_modifier_groups? In the case where I have menu_modifier_groups[0], menu_modifier_groups[1] etc...

Any help/guidance appreciated.

1 Answer 1

2

You just need to have 2 forEachs nested inside each other - loop through the groups, then loop through the items inside the current group:

$scope.calculatePrice = function(orderItem) {
    var tot = orderItem.price;

    angular.forEach(orderItem.menu_modifier_groups, function(group) {
        angular.forEach(group.menu_modifier_items, function(item) {
            if(item.selected == true) {
                tot+=item.price;
            }
        });
    });

    return "Add for " + "₦" + tot;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.