我尝试删除数组中的多个条目。
这些条目是对象,我需要找到满足特定条件的对象。
var pending = [];
a.forEach(function(entry, index) {
if(entry.b == data) {
pending.push(index);
}
});
pending.forEach(function(entry) {
a.splice(entry, 1);
});
问题是它只删除了我想要的一半(当b = data
时),甚至删除了一些随机条目......
感谢您的帮助。
最佳答案
我假设 a
是一个对象数组,您希望对其进行过滤,以便仅保留那些 b
属性等于字符串 '数据'
。原来如此:
// 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);
引用文献:
关于javascript - 删除数组内的特定元素(对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26319146/