1

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
          }
        ]
    }
]
2
  • Please show data structure and expected results. Note that find() does not have universal browser support and requires using a polyfill Commented Feb 6, 2016 at 23:45
  • @charlietfl I've added the structure of the item. How can I do this without using find Commented Feb 6, 2016 at 23:52

2 Answers 2

1

Use standard some instead of find:

function matchCartItem(item) {
    return $scope.cart[0].cart_items.some(function(itm) {
        return itm.item_id === item.item_id;
    });
}

some checks if any item in array satisfies condition.

Sign up to request clarification or add additional context in comments.

2 Comments

What about the inner array? cart_modifier_items
I want to be able to loop through the inner array of cart_items and return a single true
0

I think, you just need to loop twice to find the good cart modifier item :

var findModifier = function(itemId, cartModifierId, cart_items){
  var itemLen = cart_items.length;
  for(var i = 0; i < itemLen; ++i){
    if(cart_items[i].id === itemId){
      var modifierLen = cart_items[i].cart_modifier_items.length;
      for(var j = 0; j < modifierLen; ++j){
        if(cart_items[i].cart_modifier_items[j].id == cartModifierId){
          return true;
        }
      }
    }
  }
  return false;
}

See in plunker : https://plnkr.co/edit/oYWL8OKbzmHUvUG0sCmi?p=preview

5 Comments

I can see you're passing many items to the function... all I can pass is the item
I only pass : your $scope.cart[0].cart_items as cart_items your "item" as itemId and the id of the cart_modifier to find. Do you want to pass only the cart_modifier_id ?
item = cart_item and the cart_modifier_items are inside the cart_item..... so item contains everything. I want to check for item inside $scope.cart[0].cart_items
Ok, but you want to : find the cart item with id = itm, and once it is done, find the cart modifier with another given id ?
Yes correct, first check if cart_item exists in $scope.cart[0].cart_items.. If it exists then check that every cart_modifier_items of the cart_item we found also exists

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.