小编典典

从JavaScript中的对象数组中删除重复项

javascript

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

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

我想知道什么是从数组中删除重复对象的最佳方法。举例来说,事物……东西将变成……

{place:"here",name:"stuff"},
{place:"there",name:"morestuff"}

阅读 403

收藏
2020-04-22

共1个答案

小编典典

一种原始方法是:

var obj = {};

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

things.thing = new Array();
for ( var key in obj )
    things.thing.push(obj[key]);
2020-04-22