문제

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?

도움이 되었습니까?

해결책

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";
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top