Javascript/Lodash -从数组中删除项目(具有相同的id)

时间:2021-08-07 22:04:28

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 :

我正在尝试从具有多个对象的数组中删除一个项,在某些情况下,它可能具有相同的id。

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

假设我想删除objective: 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..).

它确实删除了项目;但是,它将删除所有具有objectId: lQnt4dmiPs的对象,这不是需要的行为。我只想删除它一次(因为删除函数触发onclick.)。

I believe that I am missing something here or rather I should use another lodash function.

我认为我在这里漏掉了一些东西或者说我应该使用另一个lodash函数。

3 个解决方案

#1


4  

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

Lodash没有一个直接的功能(Lodash #1114作为wontfix关闭),但是您可以使用_。findIndex和数组#接头:

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

#2


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);

#3


0  

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

使用findIndex,它只查找第一个实例。然后筛选数组以删除索引号的元素。

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"))

#1


4  

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

Lodash没有一个直接的功能(Lodash #1114作为wontfix关闭),但是您可以使用_。findIndex和数组#接头:

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

#2


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);

#3


0  

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

使用findIndex,它只查找第一个实例。然后筛选数组以删除索引号的元素。

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"))