1const addresses = [...]; // Some array I got from async call
2
3const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
4 .map(id => {
5 return addresses.find(a => a.id === id)
6 })
1let days = ["senin","senin","selasa","selasa","rabu","kamis", "rabu"];
2let fullname = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"},{name: "jane"}];
3
4// REMOVE DUPLICATE FOR ARRAY LITERAL
5const arrOne = new Set(days);
6console.log(arrOne);
7
8const arrTwo = days.filter((item, index) => days.indexOf(item) == index);
9console.log(arrTwo);
10
11
12// REMOVE DUPLICATE FOR ARRAY OBJECT
13const arrObjOne = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
14console.log(arrObjOne);
15
16const arrObjTwo = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
17console.log(arrObjTwo);
1function getUnique(arr, comp) {
2
3 // store the comparison values in array
4 const unique = arr.map(e => e[comp])
5
6 // store the indexes of the unique objects
7 .map((e, i, final) => final.indexOf(e) === i && i)
8
9 // eliminate the false indexes & return unique objects
10 .filter((e) => arr[e]).map(e => arr[e]);
11
12 return unique;
13}
14
15console.log(getUnique(arr,'id'));
1arr.reduce((acc, current) => {
2 const x = acc.find(item => item.id === current.id);
3 if (!x) {
4 return acc.concat([current]);
5 } else {
6 return acc;
7 }
8}, []);
1const things = {
2 thing: [
3 { place: 'here', name: 'stuff' },
4 { place: 'there', name: 'morestuff1' },
5 { place: 'there', name: 'morestuff2' },
6 ],
7};
8
9const removeDuplicates = (array, key) => {
10 return array.reduce((arr, item) => {
11 const removed = arr.filter(i => i[key] !== item[key]);
12 return [...removed, item];
13 }, []);
14};
15
16console.log(removeDuplicates(things.thing, 'place'));
17// > [{ place: 'here', name: 'stuff' }, { place: 'there', name: 'morestuff2' }]
18