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

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

当前回答

任何对象数组的泛型:

/**
* Remove duplicated values without losing information
*/
const removeValues = (items, key) => {
  let tmp = {};

  items.forEach(item => {
    tmp[item[key]] = (!tmp[item[key]]) ? item : Object.assign(tmp[item[key]], item);
  });
  items = [];
  Object.keys(tmp).forEach(key => items.push(tmp[key]));

  return items;
}

希望这对任何人都有帮助。

其他回答

TypeScript解决方案

这将删除重复的对象,并保留对象的类型。

function removeDuplicateObjects(array: any[]) {
  return [...new Set(array.map(s => JSON.stringify(s)))]
    .map(s => JSON.parse(s));
}

这是我的解决方案,将实际数组添加到键值对象中,其中键将是唯一标识,值可以是对象或整个对象的任何属性。

说明:具有重复项的主数组将转换为键/值对象如果Id已存在于唯一对象中,则该值将被覆盖。最后,只需将唯一对象转换为数组。

getUniqueItems(array) {       
        const unique = {};
        // here we are assigning item.name but it could be a complete object.
        array.map(item => unique[item.Id] = item.name);
        // here you can transform your array item like {text: unique[key], value: key} but actually you can do what ever you want
        return Object.keys(unique).map(key => ({text: unique[key], value: key}));
      })
    );
  }

常量=[{地点:“这里”,名称:“东西”},{地点:“there”,名称:“morestuff”},{地点:“there”,名称:“morestuff”}];constfilteredArr=things.reduce((thing,current)=>{const x=thing.find(item=>item.place==current.place);如果(!x){return thing.concat([current]);}其他{归还物品;}}, []);console.log(filteredArr)

通过设置对象解决方案|根据数据类型

const seed=new Set();常量=[{地点:“这里”,名称:“东西”},{地点:“there”,名称:“morestuff”},{地点:“there”,名称:“morestuff”}];constfilteredArr=things.filter(el=>{const duplicate=已看到。有(el.place);见添加(el.place);回来复制});console.log(filteredArr)

设置对象特征

Set Object中的每个值都必须是唯一的,将检查值是否相等

根据数据类型(无论是原始值还是对象引用)设置对象存储唯一值的目的。它有四个非常有用的实例方法add、clear、has和delete。

唯一的数据类型功能(&D):。。

加法

默认情况下,它将唯一数据推送到集合中,并保留数据类型。。这意味着它可以防止将重复项推入集合,并且默认情况下还会检查数据类型。。。

has方法

有时需要检查数据项是否存在于集合和中。这是集合检查唯一id或项和数据类型的简便方法。。

删除方法

它将通过标识数据类型从集合中删除特定项。。

清除方法

它将从一个特定变量中删除所有集合项,并将其设置为空对象

Set对象还具有迭代方法和更多功能。。

更好地从这里阅读:Set-JavaScript | MDN

让myData=[{place:“here”,name:“stuff”},{地点:“there”,名称:“morestuff”},{地点:“there”,名称:“morestuff”}];let q=[…new Map(myData.Map(obj=>[JSON.stringify(obj),obj]).values()];控制台日志(q)

一个使用ES6和new Map()的命令行。

// assign things.thing to myData
let myData = things.thing;

[...new Map(myData.map(obj => [JSON.stringify(obj), obj])).values()];

详细信息:-

对数据列表执行.map()并将每个单独的对象转换为[key,value]对数组(长度=2),第一个元素(key)将是对象的字符串化版本,第二个元素(value)将是一个对象本身。将上述创建的数组列表添加到新的Map()中会将键作为字符串化对象,任何相同的键添加都会导致覆盖现有的键。使用.values()将为MapIterator提供Map中的所有值(在本例中为obj)最后,传播。。。运算符为新数组提供上述步骤中的值。

我认为最好的方法是使用reduce和Map对象。这是单线解决方案。

常量数据=[{id:1,名称:“David”},{id:2,名称:“Mark”},{id:2,名称:“Lora”},{id:4,名称:“Tyler”},{id:4,名称:“Donald”},{id:5,名称:“Adrian”},{id:6,姓名:“Michael”}]constuniqueData=[…data.reduce((map,obj)=>map.set(obj.id,obj),new map()).values()];console.log(uniqueData)/*在`map.set(obj.id,obj)中`“obj.id”是键。(别担心。我们将只使用.values()方法获取值)“obj”是整个对象。*/