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

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

当前回答

常量=[{地点:“这里”,名称:“东西”},{地点:“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

其他回答

您还可以创建一个通用函数,该函数将根据传递给该函数的对象键过滤数组

function getUnique(arr, comp) {

  return arr
   .map(e => e[comp])
   .map((e, i, final) => final.indexOf(e) === i && i)  // store the keys of the unique objects
   .filter(e => arr[e]).map(e => arr[e]); // eliminate the dead keys & store unique objects

 }

你可以这样调用函数,

getUnique(things.thing,'name') // to filter on basis of name

getUnique(things.thing,'place') // to filter on basis of place

这是一种带有Set和一些闭包的单循环方法,以防止在函数声明之外使用声明的变量,并获得简短的外观。

常量array=[{地点:“here”,名称:“stuff”,n:1},{地方:“there”,名称“morestuff”,keys=['place','name'],unique=阵列过滤器((s=>o=>(v=>!s.has(v)&&s.add(v))(keys.map(k=>o[k]).join('|')))(新设置));console.log(唯一);.作为控制台包装{最大高度:100%!重要;顶部:0;}

    function genFilterData(arr, key, key1) {
      let data = [];
      data = [...new Map(arr.map((x) => [x[key] || x[key1], x])).values()];
    
      const makeData = [];
      for (let i = 0; i < data.length; i += 1) {
        makeData.push({ [key]: data[i][key], [key1]: data[i][key1] });
      }
    
      return makeData;
    }
    const arr = [
    {make: "here1", makeText:'hj',k:9,l:99},
    {make: "here", makeText:'hj',k:9,l:9},
    {make: "here", makeText:'hj',k:9,l:9}]

      const finalData= genFilterData(data, 'Make', 'MakeText');
    
        console.log(finalData);

在这里,我找到了一个使用reduce方法从对象数组中删除重复项的简单解决方案。我正在根据对象的位置键过滤元素

常量med=[{name:“name1”,位置:“left”},{name:“name2”,位置:“right”},{name:“name3”,位置:“left”},{name:“name4”,位置:“right”},{name:“name5”,位置:“left”},{name:“name6”,位置:“left1”}]常量arr=[];med.reduce((acc,curr)=>{如果(acc.indexOf(当前位置)==-1){acc.push(当前位置);arr.push(当前);}返回acc;}, [])控制台日志(arr)

基本方法是:

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