小编典典

如何从对象数组中删除所有重复项?

all

我有一个包含对象数组的对象。

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"}

阅读 133

收藏
2022-03-03

共1个答案

小编典典

一个原始的方法是:

const obj = {};

for (let i = 0, len = things.thing.length; i < len; i++) {
  obj[things.thing[i]['place']] = things.thing[i];
}

things.thing = new Array();

 for (const key in obj) { 
   things.thing.push(obj[key]);
}
2022-03-03