I have an object array:
我有一个对象数组:
[
{ name: 'one', value: '1' },
{ name: 'two', value: '22' },
{ name: 'three', value: '333' },
add: [Function],
delete: [Function]
]
How can I delete an object with name: 'two'
?
如何删除名称为“两个”的对象?
[
{ name: 'one', value: '1' },
{ name: 'three', value: '333' },
add: [Function],
delete: [Function]
]
I've tried splice()
and delete
, but they don't work in my case.
我已经尝试过splice()和删除,但它们在我的情况下不起作用。
Also tried to iterate the whole array and rebuild it depending on what I want to remove, but that doesn't seems like a good approach...
还尝试迭代整个数组并根据我想删除的内容重建它,但这似乎不是一个好方法......
Generally, I want to implement something like an ArrayList
to allow easy find/add/remove/modify.
通常,我想实现像ArrayList这样的东西,以便于查找/添加/删除/修改。
Maybe I structure my code wrong?
也许我的代码结构错了?
1 个解决方案
#1
2
You can use the .filter()
method, which returns a new array containing only the items that pass the test:
您可以使用.filter()方法,该方法返回一个只包含通过测试的项目的新数组:
var arr = arr.filter(function (obj) {
return obj.name !== "two";
});
#1
2
You can use the .filter()
method, which returns a new array containing only the items that pass the test:
您可以使用.filter()方法,该方法返回一个只包含通过测试的项目的新数组:
var arr = arr.filter(function (obj) {
return obj.name !== "two";
});