I am trying to remove an item from an array that has several objects, where in some cases it may has the same id. Here is the array :
var array = [{
"outletId": "OjHJ104",
"items": [{
"objectId": "lQnt4dmiPs",
"inCart": false,
},
{
"objectId": "lQnt4dmiPs",
"inCart": true,
},
{
"objectId": "lQnt4dmiPs",
"inCart": false,
},
{
"objectId": "lC6C96Ekua",
"inCart": true,
},
{
"objectId": "lC6C96Ekua",
"inCart": false,
},
{
"objectId": "lC6C96Ekua",
"inCart": true,
}
]
}];
Let's say I want to remove the item with the objectId : lQnt4dmiPs
Using the below :
_.remove(array, { objectId: 'lQnt4dmiPs' });
It's true that it removes the item; however it removes all the objects that has the objectId : lQnt4dmiPs which is not the wanted behavior. I want to remove it only once (as the remove function triggers onclick..).
I believe that I am missing something here or rather I should use another lodash function.
-and+(an add to cart button) when the user clicks on-it should reduce the quantity of that item from the cart meaning that it should remove only one item from the the cart that has the sameidto reduce its quantity._.findIndexto get the index of the first item with matching id, then useArray.splicemethod to remove only that first item?