如何删除数组内对象的重复值?

时间:2019-03-21 19:51:56

标签: javascript arrays json

我发现了一些有关如何删除对象thisthisthis等中的重复值的参考...在所有这些示例中(以及我发现的其他示例中)用于简单对象,但我的情况更复杂。

我有一个数组,其中每个索引都有您的值和一个对象。我创建一个简单的示例进行解释:

var data = [
  {
    "name": "Kyle",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 2,
        "name": "name2"
      },
      {
        "id": 3,
        "name": "name3"
      },
      {
        "id": 2,
        "name": "name2"
      }
      {
        "id": 3,
        "name": "name3"
      }
    ]
  },
  {
    "name": "Liza",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "John",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 4,
        "name": "name4"
      },
      {
        "id": 4,
        "name": "name4"
      },
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "Melissa",
    "item": [
      {
        "id": 2,
        "name": "name2"
      }
    ]
  }
]

如何查看,在我的item对象中,我有重复的值...我想在我的对象中删除这些重复的值。

一种方法(我认为)是复制列表并使用forEach / map / filter,但我不知道该怎么做。做吧。

我尝试了这个,但没有用:

let dataCopy = data;

dataCopy.forEach(dataItem => {
  listPrev.forEach(dataPrev => {

    //??

    //and or...
    if(dataPrev.item.id == dataItem.item.id){          
      //??

    }
  })
})

我正在尝试获得这种回报:

var data = [
  {
    "name": "Kyle",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 2,
        "name": "name2"
      },
      {
        "id": 3,
        "name": "name3"
      },
    ]
  },
  {
    "name": "Liza",
    "item": [
      {
        "id": 1,
        "name": "name1"
      }
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "John",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 4,
        "name": "name4"
      },
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "Melissa",
    "item": [
      {
        "id": 2,
        "name": "name2"
      }
    ]
  }
]

我该怎么做? 有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

简单的forEachfilter可以做到:

data.forEach(d => {
    d.item = d.item.filter((item, index, self) => {
        return index === self.findIndex(z => z.id == item.id)
    });
});

答案 1 :(得分:0)

使用Array.map()迭代数组,并将数组唯一函数应用于每个item数组:

const data = [{"name":"Kyle","item":[{"id":1,"name":"name1"},{"id":2,"name":"name2"},{"id":3,"name":"name3"},{"id":2,"name":"name2"},{"id":3,"name":"name3"}]},{"name":"Liza","item":[{"id":1,"name":"name1"},{"id":1,"name":"name1"},{"id":2,"name":"name2"}]},{"name":"John","item":[{"id":1,"name":"name1"},{"id":1,"name":"name1"},{"id":4,"name":"name4"},{"id":4,"name":"name4"},{"id":2,"name":"name2"}]},{"name":"Melissa","item":[{"id":2,"name":"name2"}]}];

const uniqueById = arr => Object.values(arr.reduce((r, o) => ({
  [o.id]: o,
  ...r
}), {}));

const result = data.map(o => ({
  ...o,
  item: uniqueById(o.item)
}));

console.log(result);

答案 2 :(得分:0)

您可以编写一个实用函数,然后按如下所示映射集合:

var dedupeBy = (arr, by) =>
  Object.values(arr.reduce((a, item) => ({ ...a, [item[by]]: item }), {}));
data.map((person) => ({ ...person, item: dedupeBy(person.item, 'name') }));