gpt4 book ai didi

javascript - 从对象数组中删除对象

转载 作者:IT王子 更新时间:2023-10-29 03:15:04 25 4
gpt4 key购买 nike

我有一个对象数组:

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

我如何删除这个项目 {"value":"14","label":"7"} 产生新数组:

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

?

最佳答案

在 ES6 中(或使用 es6-shim )你可以使用 Array.prototype.findIndex连同 Array.prototype.splice :

arr.splice(arr.findIndex(matchesEl), 1);

function matchesEl(el) {
return el.value === '14' && el.label === '7';
}

或者如果数组的副本没问题(自 ES5 起可用),Array.prototype.filter这是要走的路:

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

关于javascript - 从对象数组中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com