javascript Lodash 通过匹配 ids 数组从数组中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48077122/
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:46:40  来源:igfitidea点击:

Lodash Remove objects from array by matching ids array

javascriptarrayslodash

提问by Gitesh Purbia

I have an array of objects like:

我有一个对象数组,例如:

var a = [
  {id: 1, name: 'A'},
  {id: 2, name: 'B'},
  {id: 3,  name: 'C'},
  {id: 4, name: 'D'}
];

And Ids array which i want to remove from array a :

还有我想从数组 a 中删除的 Ids 数组:

var removeItem = [1,2];

I want to remove objects from array aby matching its ids, which removeItem arraycontains. How can i implement with lodash.

我想array a通过匹配其removeItem array包含的id来删除对象。如何使用 lodash 实现。

I Checked lodash's _.removemethod, but this need a specific condition to remove an item from array. But i have list of ids which i want to remove.

我检查了 lodash 的_.remove方法,但这需要特定条件才能从数组中删除项目。但是我有要删除的 ID 列表。

采纳答案by Gabriele Petrioli

As you mentioned you need the _.removemethod and the specific condition you mention is whether the removeItemarray contains the idof the checked element of the array.

正如您提到的,您需要该_.remove方法,您提到的具体条件是removeItem数组是否包含数组id的已检查元素的 。

var removeElements = _.remove(a, obj => removeItem.includes(obj.id));
// you only need to assign the result if you want to do something with the removed elements.
// the a variable now holds the remaining array

回答by Mihai Alexandru-Ionut

You have to pass a predicatefunction to .removemethod from lodash.

您必须将predicate函数传递给.remove方法 from lodash

var final = _.remove(a, obj => removeItem.indexOf(obj.id) > -1);

using indexOfmethod.

使用indexOf方法。

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

indexOf() 方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。

You can do it using nativejavascript using filtermethod which accepts as parameter a callbackfunction.

您可以使用接受回调函数作为参数的方法使用nativejavascript 来完成。filter

var a = [
  {id: 1, name: 'A'},
  {id: 2, name: 'B'},
  {id: 3,  name: 'C'},
  {id: 4, name: 'D'}
];
var removeItem = [1,2];
a = a.filter(function(item){ 
   return removeItem.indexOf( item.id ) == -1; 
});
console.log(a);

But filtermethod just creates a new array by applying a callbackfunction.

但是filter方法只是通过应用回调函数来创建一个新数组。

From documentation:

文档

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

filter() 方法创建一个新数组,其中包含通过提供的函数实现的测试的所有元素。

If you want to modify the original array use splicemethod.

如果要修改原始数组使用splice方法。

var a = [
  {id: 1, name: 'A'},
  {id: 2, name: 'B'},
  {id: 3,  name: 'C'},
  {id: 4, name: 'D'}
];
var removeItem = [1,2];
removeItem.forEach(function(id){
   var itemIndex = a.findIndex(i => i.id == id);
   a.splice(itemIndex,1);
});
console.log(a);