-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path2-distinct.js
More file actions
29 lines (23 loc) · 635 Bytes
/
Copy path2-distinct.js
File metadata and controls
29 lines (23 loc) · 635 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use strict';
const distinct = (dataset) => {
const keys = new Set();
return dataset.filter((record) => {
const cols = Object.keys(record).sort();
const key = cols.map((field) => record[field]).join('\x00');
const has = keys.has(key);
if (!has) keys.add(key);
return !has;
});
};
// Usage
const flights = [
{ from: 'Kiev', to: 'Rome' },
{ from: 'Kiev', to: 'Warsaw' },
{ from: 'Dublin', to: 'Riga' },
{ from: 'Riga', to: 'Dublin' },
{ from: 'Kiev', to: 'Rome' },
{ from: 'Cairo', to: 'Paris' },
];
console.dir({ flights });
const directions = distinct(flights);
console.dir({ directions });