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

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

问题描述

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

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 = [{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()Array.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天全站免登陆