0

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?

8
  • 1
    In your initial array, is arr[3] the function add? Commented Aug 9, 2013 at 14:55
  • 3
    You appear to be using object literal notation inside an array - is this intentional? Commented Aug 9, 2013 at 14:55
  • possible duplicate of Adding/removing items from JSON data with JQuery Commented Aug 9, 2013 at 14:56
  • @Qantas94Heavy - It definitely sounds intentional... "remove an object from array with objects and functions". Commented Aug 9, 2013 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]). Commented Aug 9, 2013 at 15:01

1 Answer 1

2

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";
});
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and elegant, with the proviso that he remove the object notation which is breaking his array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.