简体   繁体   English

删除数组内的特定元素(对象)

[英]Deleting specific elements (objects) inside array

I've tried to delete multiple entries in an array. 我试图删除数组中的多个条目。

These entries are objects and I need to locate the object meeting specific criteria. 这些条目是对象,我需要找到满足特定条件的对象。

    var pending = [];
    a.forEach(function(entry, index) {
        if(entry.b == data) {
            pending.push(index);
        }
    });
    pending.forEach(function(entry) {
        a.splice(entry, 1);
    });

The problem is that it only deletes half of what I want (when b = data ) and even deletes some random entries... 问题是它只会删除我想要的一半(当b = data ),甚至删除一些随机条目...

Thank you for your help. 谢谢您的帮助。

I'm assuming that a is an array of objects, that you want to filter in order to keep only those whose b property is equal to the string 'data' . 我假设a是一个对象数组,您希望对其进行过滤,以仅保留b属性等于字符串'data' That being the case: 既然如此:

 // this outputs to the console, you should probably press 'F12' var a = [{ 'b': 'data' }, { 'b': 'something else' }, { 'b': 'data' }, { 'b': 50 }], pending = a.filter(function(elem) { return elem.b === 'data'; }); console.log(pending); 

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM