Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
In your initial array, is arr[3] the function add? –  Jon Aug 9 '13 at 14:55
3  
You appear to be using object literal notation inside an array - is this intentional? –  Qantas 94 Heavy Aug 9 '13 at 14:55
    
possible duplicate of Adding/removing items from JSON data with JQuery –  Someone Somewhere Aug 9 '13 at 14:56
    
@Qantas94Heavy - It definitely sounds intentional... "remove an object from array with objects and functions". –  James Allardice Aug 9 '13 at 14:57
1  
@Qantas94Heavy - Oh, sorry, I see what you mean now. Yeah that must be a typo in the question (perhaps copied from the console, since it also just says [Function]). –  James Allardice Aug 9 '13 at 15:01

1 Answer 1

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";
});
share|improve this answer
    
Nice and elegant, with the proviso that he remove the object notation which is breaking his array. –  shennan Aug 9 '13 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.