Remove object from array of objects

前端 未结 9 1572
滥情空心
滥情空心 2021-01-31 15:28

I have an array of objects:

[{\"value\":\"14\",\"label\":\"7\"},{\"value\":\"14\",\"label\":\"7\"},{\"value\":\"18\",\"label\":\"7\"}]

How I ca

9条回答
  •  半阙折子戏
    2021-01-31 15:45

    In ES6 (or using es6-shim) you can use Array.prototype.findIndex along with Array.prototype.splice:

    arr.splice(arr.findIndex(matchesEl), 1);
    
    function matchesEl(el) {
        return el.value === '14' && el.label === '7';
    }
    

    Or if a copy of the array is ok (and available since ES5), Array.prototype.filter's the way to go:

    var withoutEl = arr.filter(function (el) { return !matchesEl(el); });
    

提交回复
热议问题