Javascript 从对象数组中删除对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29997710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove object from array of objects
提问by Arthur Yakovlev
I have an array of objects:
我有一个对象数组:
[{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]
How I can delete this item {"value":"14","label":"7"}resulting in the new array:
如何删除此项目以{"value":"14","label":"7"}生成新数组:
[{"value":"14","label":"7"},{"value":"18","label":"7"}]
?
?
采纳答案by Edgar Orozco
回答by Noah Freitas
In ES6 (or using es6-shim) you can use Array.prototype.findIndexalong with Array.prototype.splice:
在 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';
}
Or if a copy of the array is ok (and available since ES5), Array.prototype.filter's the way to go:
或者如果数组的副本没问题(并且自 ES5 起可用),Array.prototype.filter是要走的路:
var withoutEl = arr.filter(function (el) { return !matchesEl(el); });
回答by superluminary
Solution with Identity
身份解决方案
If you have object identity not just object equality (i.e. you're trying to delete a specific object from the array, not just an object that contains the same data as an existing object) you can do this very simply with splice and indexOf:
如果您有对象标识而不仅仅是对象相等(即您试图从数组中删除特定对象,而不仅仅是包含与现有对象相同数据的对象),您可以使用 splice 和 indexOf 非常简单地做到这一点:
a = {x:1}
b = {x:2}
arr = [a,b]
Say you want to remove b:
假设您要删除 b:
arr.splice(
arr.indexOf(b), 1
);
Solution with Equality
平等的解决方案
The question makes it unclear whether we are dealing with identity or equality. If you have equality (two objects containing the same stuff, which look the same as each other, but are not actually the same object), you'll need a little comparison function, which we can pass to findIndex.
这个问题使我们不清楚我们是在处理身份还是平等。如果您有相等性(包含相同内容的两个对象,它们看起来彼此相同,但实际上不是同一个对象),您将需要一个小的比较函数,我们可以将其传递给 findIndex。
a = {x: 1};
b = {x: 2};
arr = [a, b];
Now we want to remove c, which is equal to, but not the same as a:
现在我们要删除 c,它等于但不等于 a:
c = {x: 1}
index = arr.findIndex(
(i) => i.x === c.x
)
if (index !== -1) {
arr.splice(
index, 1
);
}
Note that I'm passing an ES6 style lambda function here as the first parameter to findIndex.
请注意,我在这里传递了一个 ES6 风格的 lambda 函数作为 findIndex 的第一个参数。
Unless you have the luxury of only writing for evergreen browsers, you'll probably want to be using a transpiler like Babel to use this solution.
除非您有能力只为常绿浏览器编写代码,否则您可能希望使用像 Babel 这样的转译器来使用此解决方案。
Solution with Immutability
具有不变性的解决方案
If you care about immutability, and you want to return a new array, you can do it with filter:
如果您关心不变性,并且想要返回一个新数组,则可以使用 filter 来实现:
a = {x: 1};
b = {x: 2};
arr = [a, b];
Let's keep everything that isn't a:
让我们保留所有不是:
newArr = arr.filter((x) => x != a)
回答by Jimmy Obonyo Abor
If you dont want to mutate your array simply use filter method , Array.prototype.filter()
如果你不想改变你的数组,只需使用 filter 方法, Array.prototype.filter()
array.filter((i)=> i != "b");
array.filter((i)=> i != "b");
回答by Jimmy Obonyo Abor
I would create a new array...
我会创建一个新数组...
var original = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]
var result = [];
for (var i = 0, l = original.length; i < l; i++) { // Traverse the whole array
var current = original[i];
if (! (current.value == 14 && current.label == 7) ) {
// It doesn't match the criteria, so add it to result
result.push( current );
}
}
Edit: I've read your question once more. You only want to remove the first element? Then use sliceto get only a part of the array.
编辑:我再次阅读了您的问题。您只想删除第一个元素?然后使用slice仅获取数组的一部分。
var result = original.slice(1, original.length)
or splice
或者 splice
original.splice(0,1); // Remove one element from index 0.
回答by José Antonio Postigo
Using ES6 approach:
使用 ES6 方法:
const objIndex = objs.findIndex(o => o.value === '14' && o.label === '7')
if(objIndex > -1) {
objs.slice(objIndex, 1)
}
回答by datchung
Here is a way to do it without 'creating a new array' or using external libraries. Note that this will only remove the first instance found, not duplicates, as stated in your question's example.
这是一种无需“创建新数组”或使用外部库的方法。请注意,这只会删除找到的第一个实例,而不是重复项,如您的问题示例中所述。
// Array to search
var arr = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}];
// Object to remove from arr
var remove = {"value":"14","label":"7"};
// Function to determine if two objects are equal by comparing
// their value and label properties
var isEqual = function(o1, o2) {
return o1 === o2 ||
(o1 != null && o2 != null &&
o1.value === o2.value &&
o1.label === o2.label);
};
// Iterate through array and determine if remove is in arr
var found = false;
var i;
for(i = 0; i < arr.length; ++i) {
if(isEqual(arr[i], remove)) {
found = true;
break;
}
}
if(found) {
// Found remove in arr, remove it
arr.splice(i, 1);
}
回答by xiaolu wang
function removeDuplicateObject (arr) {
let temp = arr.map((item) => {
return JSON.stringify(item);
});
temp = Array.from(new Set(temp));
return temp.map((item) => {
return JSON.parse(item);
});
}
Then removeDuplicateObject(your array)
然后 removeDuplicateObject(your array)
回答by Kang
Do you need to keep the duplicate object?
你需要保留重复的对象吗?
With underscoreJS
带下划线
var oldarray = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]
var newarray = _.reject(oldarray, function(o) {
return o == {"value":"14","label":"7"};
});

