过滤并删除数组中的过滤元素 [英] Filter and delete filtered elements in a array

查看:114
本文介绍了过滤并删除数组中的过滤元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除原始数组中的特定元素( var a )。我 filter()该数组和 splice()返回了新数组。但这对此代码中的原始数组没有影响。如何从原始数组中轻松删除这些元素?

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

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


推荐答案

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

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.

如果你想查找和删除你的阵列中的一个项目,就应该这样做

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

无论您选择的列表中有多少元素,这都可以完成您的工作。但我相信虽然这看起来合乎逻辑,但它确实有大量的冗余工作。第一个过滤器然后每个过滤后的元素会对此进行索引搜索。虽然我知道findIndex很快就疯了但我会发现这个结果显然很慢,特别是对于大数组而言。我们找一个o(n)解决方案。你去吧

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

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

所以必须这样。

这篇关于过滤并删除数组中的过滤元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆