ES6 - 基于两个键的独特元素

时间:2018-03-07 14:07:47

标签: javascript ecmascript-6

在以下数组中,有重复的元素(具有相同的日期和ID)

[{date: "2018-02-20", id: "ccff5cae-0d38-450d-90f9-2490256db84c"},
{date: "2018-02-21", id: "8ffc7266-6a19-40a7-b181-7c7d73be8467"},
{date: "2018-02-22", id: "147afdd1-8c8b-4fde-b992-d8d8465a1771"},
{date: "2018-02-23", id: "72f10683-17a5-49d1-9e78-5fc2b380a314"},
{date: "2018-02-24", id: "2d2cd12c-2472-4e1d-96c9-84d8a0df1eda"},
{date: "2018-03-05", id: "5daf20a7-7106-419d-9761-3e96dfedf5ff"},
{date: "2018-03-08", id: "4bbcc474-ff88-4fc0-aede-abde0ae42b93"},
{date: "2018-03-08", id: "2a883953-df6a-45b1-9e0a-eb67cc1752b3"},
{date: "2018-03-08", id: "578b78cb-6d2c-4e91-a5a1-6d081945a51b"},
{date: "2018-03-09", id: "defb35e5-1a43-4347-ac4b-a85093551900"},
{date: "2018-02-20", id: "ccff5cae-0d38-450d-90f9-2490256db84c"},
{date: "2018-02-21", id: "8ffc7266-6a19-40a7-b181-7c7d73be8467"},
{date: "2018-02-22", id: "147afdd1-8c8b-4fde-b992-d8d8465a1771"},
{date: "2018-02-23", id: "72f10683-17a5-49d1-9e78-5fc2b380a314"},
{date: "2018-02-24", id: "2d2cd12c-2472-4e1d-96c9-84d8a0df1eda"},
{date: "2018-03-05", id: "5daf20a7-7106-419d-9761-3e96dfedf5ff"},
{date: "2018-03-08", id: "4bbcc474-ff88-4fc0-aede-abde0ae42b93"},
{date: "2018-03-08", id: "2a883953-df6a-45b1-9e0a-eb67cc1752b3"},
{date: "2018-03-08", id: "578b78cb-6d2c-4e91-a5a1-6d081945a51b"},
{date: "2018-03-09", id: "defb35e5-1a43-4347-ac4b-a85093551900"},]

删除重复项的最佳方式(在ES6中)是什么?

2 个答案:

答案 0 :(得分:1)

不完全是ES6,但有一种方法是使用Lodash uniq函数

_.uniq([2, 1, 2]);
// => [2, 1]

编辑:UniqBy可能更符合您的目标:

// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

答案 1 :(得分:1)

您可以使用地图,并在日期和时间之外创建一个唯一的密钥。 ID ..

以下是一个例子。

var data =
[{date: "2018-02-20", id: "ccff5cae-0d38-450d-90f9-2490256db84c"},
{date: "2018-02-21", id: "8ffc7266-6a19-40a7-b181-7c7d73be8467"},
{date: "2018-02-22", id: "147afdd1-8c8b-4fde-b992-d8d8465a1771"},
{date: "2018-02-23", id: "72f10683-17a5-49d1-9e78-5fc2b380a314"},
{date: "2018-02-24", id: "2d2cd12c-2472-4e1d-96c9-84d8a0df1eda"},
{date: "2018-03-05", id: "5daf20a7-7106-419d-9761-3e96dfedf5ff"},
{date: "2018-03-08", id: "4bbcc474-ff88-4fc0-aede-abde0ae42b93"},
{date: "2018-03-08", id: "2a883953-df6a-45b1-9e0a-eb67cc1752b3"},
{date: "2018-03-08", id: "578b78cb-6d2c-4e91-a5a1-6d081945a51b"},
{date: "2018-03-09", id: "defb35e5-1a43-4347-ac4b-a85093551900"},
{date: "2018-02-20", id: "ccff5cae-0d38-450d-90f9-2490256db84c"},
{date: "2018-02-21", id: "8ffc7266-6a19-40a7-b181-7c7d73be8467"},
{date: "2018-02-22", id: "147afdd1-8c8b-4fde-b992-d8d8465a1771"},
{date: "2018-02-23", id: "72f10683-17a5-49d1-9e78-5fc2b380a314"},
{date: "2018-02-24", id: "2d2cd12c-2472-4e1d-96c9-84d8a0df1eda"},
{date: "2018-03-05", id: "5daf20a7-7106-419d-9761-3e96dfedf5ff"},
{date: "2018-03-08", id: "4bbcc474-ff88-4fc0-aede-abde0ae42b93"},
{date: "2018-03-08", id: "2a883953-df6a-45b1-9e0a-eb67cc1752b3"},
{date: "2018-03-08", id: "578b78cb-6d2c-4e91-a5a1-6d081945a51b"},
{date: "2018-03-09", id: "defb35e5-1a43-4347-ac4b-a85093551900"}];

const d = Array.from(new Map(data.map((v) => {
  return [v.date + v.id, v];
})).values());

console.log(`Old length: ${data.length}`);
console.log(`New length: ${d.length}`);
console.log(d);