I have a function in angularJS that uses find to check if an item exists in an array;
function matchCartItem(item) {
return $scope.cart[0].cart_items.find(function(itm) {
return itm.item_id === item.item_id
});
}
As you can see I pass an item to the function then i check if the item_id of the passed item can be found in the items in the cart_items array
This works great but now I want to modify it.
cart_items has an inner array cart_modifier_items
. When a cart_item is passed it contains cart_modifier_items. My function at the moment only checks for matching cart_item
How can I change this function to also check the cart_modifier_items?
So I want to check for matching item.item_id in $scope.cart[0].cart_items -- my function does this
But also check for matching item.cart_modifier_item[i] in$scope.cart[0].cart_items[i].cart_modifier_itemswhereiis like looping through all thecart_items`
Any help/guidance appreciated
Data structure of item
cart_item: [
{
"id": 159,
"item_id": 20,
"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,
"cart_modifier_items": [
{
"id": 34,
"item_id": 29,
"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
},
{
"id": 35,
"item_id": 10,
"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
}
]
}
]
find()does not have universal browser support and requires using a polyfillitem. How can I do this without usingfind