javascript javascript删除对象数组的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48269545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:55:58  来源:igfitidea点击:

javascript remove multiple values of array of objects

javascriptecmascript-6

提问by Kiow

I am new to javascript and I am trying to remove multiple values from an array of objects.

我是 javascript 新手,我正在尝试从对象数组中删除多个值。

Right now I can remove one object like this

现在我可以像这样删除一个对象

if(obj.findObjectByKey(response, 'cat_id', 171) == true) {
    var catId = response.map(item => item.cat_id).indexOf(171);
}

In the code above I will remove the item with a id of "171" but now I have an array with multiple values that I would like to remove.

在上面的代码中,我将删除 id 为“171”的项目,但现在我有一个包含多个值的数组,我想删除它。

So how can I modify the code above to let me pass in an array of items that I want to remove from the list eg ['171', '172', '173, '174'];

那么如何修改上面的代码,让我传入一个我想从列表中删除的项目数组,例如 ['171', '172', '173, '174'];

回答by Devan Buggay

If you can use ES6, try using JavaScript's filterand includesfor this.

如果您可以使用 ES6,请尝试使用 JavaScript 的过滤器并为此包含

const nums = [1, 2, 3, 4, 5, 6];
const remove = [1, 2, 4, 6];

function removeFromArray(original, remove) {
  return original.filter(value => !remove.includes(value));
}

console.log(removeFromArray(nums, remove));

This will work just as well with strings.

这也适用于字符串。

const nums = ["1", "2", "3", "4", "5", "6"];
const remove = ["1", "2", "4", "6"];

function removeFromArray(original, remove) {
  return original.filter(value => !remove.includes(value));
}

console.log(removeFromArray(nums, remove));

This solution also has the added benefit of being immutable. Meaning that you are returning new data rather than morphing the old. Which is considered good practice.

此解决方案还具有不可变的额外好处。这意味着您正在返回新数据而不是改变旧数据。这被认为是很好的做法。

https://wecodetheweb.com/2016/02/12/immutable-javascript-using-es6-and-beyond/

https://wecodetheweb.com/2016/02/12/immutable-javascript-using-es6-and-beyond/

回答by Jonas Wilms

 for(const id of ['171', '172', '173', '174']){
    const index = response.findIndex(item => item.cat_id === id);
    if(index !== -1)
        response.splice(index, 1);
 }

You may simply iterate over your ids and remove them.

您可以简单地遍历您的 id 并删除它们。

回答by Matthew Dumas

You just need to loop over that array, and check for the array element.

您只需要遍历该数组,并检查数组元素。

arrayToRemove = ['171', '172', '173', '174'];

for (i = arrayToRemove.length; i>=0; i--) {
   if(obj.findObjectByKey(response, 'cat_id', arrayToRemove[i]) == true) {
       var catId = response.map(item => item.cat_id).indexOf(arrayToRemove[i]);
       response.splice(catId, 1);
   } 
}

There are other ways to do this, but this is the fastest and most straight forward with least changes to your current code.

还有其他方法可以做到这一点,但这是最快、最直接的方法,对当前代码的更改最少。

It's important to note that you should loop this from the end of the array to remove to the beginning so you can preserve indices of the other elements in the remaining array.

重要的是要注意,您应该从数组的末尾循环删除到开头,以便您可以保留剩余数组中其他元素的索引。