You can use the .filter()
method, which returns a new array containing only the items that pass the test:
var arr = arr.filter(function (obj) {
return obj.name !== "two";
});
Question
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.
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.
Maybe I structure my code wrong?
Solution
You can use the .filter()
method, which returns a new array containing only the items that pass the test:
var arr = arr.filter(function (obj) {
return obj.name !== "two";
});