我试图删除数组中的多个条目。
这些条目是对象,我需要找到满足特定条件的对象。
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
属性等于字符串'data'
的对象。既然如此:
// 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);
参考文献:
Array.prototype.filter()
。关于javascript - 删除数组内的特定元素(对象),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26319146/