我有一个包含对象数组的对象。
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"}
我认为,将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]
, [])
const uniqueElements = (arr, fn) => arr.reduce((acc, v) => {
if (!acc.some(x => fn(v, x))) { acc.push(v); }
return acc;
}, []);
const stuff = [
{place:"here",name:"stuff"},
{place:"there",name:"morestuff"},
{place:"there",name:"morestuff"},
];
const unique = uniqueElements(stuff, (a,b) => a.place === b.place && a.name === b.name );
//console.log( unique );
[{
"place": "here",
"name": "stuff"
},
{
"place": "there",
"name": "morestuff"
}]