JavascriptLodash - Removing an item from an array (has the same id) - Stack Overflow

I am trying to remove an item from an array that has several objects, where in some cases it may has th

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.

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.

Share Improve this question asked Jan 9, 2018 at 21:41 Hamza L.Hamza L. 1,8234 gold badges28 silver badges47 bronze badges 8
  • 1 It returns an array of the removed elements. So, from that returned array, just remove the first one, and add them back to the original object. – user47589 Commented Jan 9, 2018 at 21:44
  • @Amy can you please extend more ? A example will be appreciated. – Hamza L. Commented Jan 9, 2018 at 21:46
  • First, can you elaborate on what you mean by " the remove function triggers onclick"? – user47589 Commented Jan 9, 2018 at 21:48
  • @Amy I have a button with a - 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 same id to reduce its quantity. – Hamza L. Commented Jan 9, 2018 at 21:51
  • 1 If you only want to remove one, you could use _.findIndex to get the index of the first item with matching id, then use Array.splice method to remove only that first item? – CRice Commented Jan 9, 2018 at 21:56
 |  Show 3 more ments

3 Answers 3

Reset to default 7

Lodash doesn't have a function to do that directly (lodash#1114 closed as wontfix), but you can use _.findIndex and Array#splice:

var index = _.findIndex(array, { objectId: 'lQnt4dmiPs' })
// The second argument is the number of items to remove.
var removedItem = array.splice(index, 1)

You can always just quickly roll your own solution, it is quite trivial.

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,
    }
  ]
 }];
  const remove = (arr, id) => {
      for(let i=0; i<arr.length;i++) {
         if(arr[i].objectId === id) {
            arr.splice(i,1);
            return;
         }
      }
  }
  remove(array[0].items,'lQnt4dmiPs');
  console.log(array);

Use findIndex, which only finds the first instance. Then filter the array to remove that index number's element.

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,
    }
  ]
 }];
 
 const remove = (arr, str) => {
   const elIdx = arr.findIndex(({objectId}) => objectId === str)
   return arr.filter((_, idx) => idx !== elIdx)
 }
 
 console.log(remove(array[0].items, "lQnt4dmiPs"))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744260199a4565593.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信