I have a function written in angularJS
that utilizes find
to verify the presence of an item in an array;
function checkCartItem(item) {
return $scope.cart[0].cart_items.find(function(itm) {
return itm.item_id === item.item_id
});
}
The function, as shown above, takes an item
as input and then checks if the item_id
of the provided item
exists in the items
within the cart_items
array.
Now, I am looking to enhance this function.
The cart_items
array contains a sub array called cart_modifier_items
. When a cart_item
is passed, it includes the cart_modifier_items
. However, my current function only looks for a match in cart_item
.
How can I modify this function to also include the cart_modifier_items
in the check?
Essentially, I want to validate if the item.item_id
matches any item in $scope.cart[0].cart_items
- which the function already does.
But, I also need to verify if the item.cart_modifier_item[i]
exists in
$scope.cart[0].cart_items[i].cart_modifier_items
where i
represents looping through all the cart_items
.
Any assistance or guidance on this matter would be highly appreciated.
Data structure of the 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
}
]
}
]