我有一个包含对象数组的对象。
obj = {};
obj.arr = new Array();
obj.arr.push({place:"here",name:"stuff"});
obj.arr.push({place:"there",name:"morestuff"});
obj.arr.push({place:"there",name:"morestuff"});
我想知道从数组中删除重复对象的最佳方法是什么。例如,obj.arr将变成。。。
{place:"here",name:"stuff"},
{place:"there",name:"morestuff"}
使用ES6“reduce”和“find”数组助手方法的简单解决方案
工作效率高,非常好!
"use strict";
var things = new Object();
things.thing = new Array();
things.thing.push({
place: "here",
name: "stuff"
});
things.thing.push({
place: "there",
name: "morestuff"
});
things.thing.push({
place: "there",
name: "morestuff"
});
// the logic is here
function removeDup(something) {
return something.thing.reduce(function (prev, ele) {
var found = prev.find(function (fele) {
return ele.place === fele.place && ele.name === fele.name;
});
if (!found) {
prev.push(ele);
}
return prev;
}, []);
}
console.log(removeDup(things));
我认为,将reduce与JSON.stringify结合起来以完美地比较对象,并选择性地添加那些尚未在累加器中的对象是一种优雅的方式。
请记住,在极端情况下,JSON.stringify可能会成为一个性能问题,因为阵列有许多对象,而且它们很复杂,但在大多数情况下,这是IMHO的最短路径。
var集合=〔{a:1},{a:2},{a:1},{a:3}〕var filtered=collection.reduce((已过滤,项)=>{if(!filtered.some(filteredItem=>JSON.stringify(filtered item)==JSON.sstringify(item)))已过滤推送(项)返回已过滤}, [])console.log(已过滤)
另一种写法相同(但效率较低):
collection.reduce((filtered, item) =>
filtered.some(filteredItem =>
JSON.stringify(filteredItem ) == JSON.stringify(item))
? filtered
: [...filtered, item]
, [])