Javascript 过滤和删除数组中的过滤元素

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

Filter and delete filtered elements in an array

javascriptarraysnode.js

提问by s1n7ax

I want to remove specific elements in the original array (which is var a). I filter()that array and splice()returned new array. but that doesn't affect the original array in this code. How can I easily remove those elements from the original array?

我想删除原始数组(即var a)中的特定元素。我filter()那个数组并splice()返回新数组。但这不会影响此代码中的原始数组。如何轻松地从原始数组中删除这些元素?

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]

var b = a.filter(function (e) {
    return e.name === 'tc_001';
});

b.splice(0,1);

console.log(a);
console.log(b);

回答by Redu

The Array.prototype.filter()method is used to collect an element set not only one item. If you would like to get one item by evaluating a condition then you have three other options. Array.prototype.indexOf(), Array.prototype.findIndex()and Array.prototype.find()Accordingly only if you want to make an operation on more than one item you should think using the filter function. None of the answers are complete in terms of the job needed to be done. They use the filter function to isolate a set (happens to be only one item in this example) but they don't show how to get rid of the whole set. Well ok let's clarify.

Array.prototype.filter()方法用于收集一个元素集,而不仅仅是一个项目。如果您想通过评估条件来获得一项,那么您还有其他三种选择。Array.prototype.indexOf()Array.prototype.findIndex()Array.prototype.find()因此只有当你想对多个项目的操作,你应该考虑使用过滤功能。就需要完成的工作而言,没有一个答案是完整的。他们使用 filter 函数来隔离一个集合(在这个例子中碰巧只有一个项目),但他们没有展示如何摆脱整个集合。好吧,让我们澄清一下。

If you want to do find and delete only one item of your array it shall be done like this

如果您只想查找和删除数组中的一项,则应这样做

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}];
a.splice(a.findIndex(e => e.name === "tc_001"),1);
console.log(a);

However since you mention "specific elements" in plural, then you will need to collect a set of selected items and do the above job one by one on each element in the set. So the proper approach would be.

但是,由于您以复数形式提及“特定元素”,因此您需要收集一组选定的项目,并对集合中的每个元素逐一执行上述工作。所以正确的方法是。

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}],
    b = a.filter(e => e.name === "tc_001");
b.forEach(f => a.splice(a.findIndex(e => e.name === f.name),1));
console.log(a);

Regardless of how many elements there are in your selected list this will do your job. Yet i believe although this looks logical it does tons of redundant job. First filters and then per filtered element does index search this and that. Although i know that findIndex is crazy fast still I would expect this one turn out to be noticeably slow especially with big arrays. Let's find an o(n) solution. Here you go

无论您选择的列表中有多少元素,这都可以完成您的工作。然而我相信,虽然这看起来合乎逻辑,但它确实做了大量的冗余工作。首先过滤,然后每个过滤的元素进行索引搜索这个和那个。虽然我知道 findIndex 非常快,但我仍然希望这个结果会明显变慢,尤其是对于大数组。让我们找到一个 o(n) 解决方案。干得好

    var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}];
    a = a.reduce((p,c) => (c.name !== "tc_001" && p.push(c),p),[]);
console.log(a);

So this must be it.

所以一定是这个。

回答by Ninja Coding

another way is filtering in two list like below:

另一种方法是在两个列表中过滤,如下所示:

const originalList = [{condition:true}, {condition: false}, {condition: true}];

// wished lists
const listWithTrue = originalList.filter(x=>x.condition);
const listWithFalse = originalList.filter(x=>!x.condition); // inverse condition

回答by Kamil W??yk

I know I'm a little bit late to the party, but how about a little bit different approach?

我知道我参加聚会有点晚了,但是稍微不同的方法怎么样?

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_002'}, {name:'tc_002'}, {name:'tc_003'}];

while ( a.findIndex(e => e.name === 'tc_002' ) >= 0 )
 a.splice( a.findIndex(f => f.name === 'tc_002'),1);
 
console.log(a);

I know it has some drawbacks, but i hope, it will help some of you in some cases :)

我知道它有一些缺点,但我希望,在某些情况下它会帮助你们中的一些人:)

cheers!

干杯!

回答by Shil Nevado

So if I understood, you want to remove the elements that matches the filter from the original array (a) but keep them in the new array (b) See if this solution is what you need:

因此,如果我理解,您想从原始数组 (a) 中删除与过滤器匹配的元素,但将它们保留在新数组中 (b) 看看这个解决方案是否是您所需要的:

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]

var b = a.filter(function (e) {
      return e.name === 'tc_002'
});

b.forEach(function(element) {
   console.log(element)
   var index = a.indexOf(element)
   console.log(index)
   a.splice(index, 1)
})

Result: a = [{"name":"tc_001"},{"name":"tc_003"}] b = [{"name":"tc_002"}]

结果:a = [{"name":"tc_001"},{"name":"tc_003"}] b = [{"name":"tc_002"}]

回答by AlexD

You can either make changes to the original array:

您可以对原始数组进行更改:

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
a = a.filter(function (e) {
    return e.name === 'tc_001';
});
a.splice(0,1);

Or in your own code just apply to aeverything you've done to b

或者在您自己的代码中只适用于a您所做的一切b

a = b

[edit]

[编辑]

Perhaps if I expand on what each action does, it will be more clear.

也许如果我扩展每个动作的作用,它会更清楚。

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
// You now have array a with 3 elements.

b = a.filter(function (e) {
    return e.name === 'tc_001';
});
// a is still 3 elements, b has one element (tc_001)

b.splice(0,1);
// You remove the first element from b, so b is now empty.

If you want to remove the elements tc_002and tc_003from abut keep them in b, this is what you can do:

如果要删除元素tc_002tc_003从中删除元素a但将它们保留在 中b,则可以执行以下操作:

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
b = a.filter(function (e) {
    return e.name != 'tc_001';
});
a = a.filter(function(item) { return b.indexOf(item) == -1; });
console.log(a); // tc_001
console.log(b); // tc_002, tc_003

回答by Suneet Bansal

You can assign filter array to a itself and then apply splice over it.

您可以将过滤器数组分配给自身,然后对其应用拼接。

Example below:

下面的例子:

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
a = a.filter(function (e) {
    return e.name === 'tc_001';
});

a.splice(0,1);