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 person = [
2{name: "john"},
3{name: "jane"},
4{name: "imelda"},
5{name: "john"},
6{name: "jane"}
7];
8
9const obj = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
10console.log(obj);
1// BEST ANSWER - LINEAR TIME SOLUTION
2
3const seen = new Set();
4const arr = [
5 { id: 1, name: "test1" },
6 { id: 2, name: "test2" },
7 { id: 2, name: "test3" },
8 { id: 3, name: "test4" },
9 { id: 4, name: "test5" },
10 { id: 5, name: "test6" },
11 { id: 5, name: "test7" },
12 { id: 6, name: "test8" }
13];
14
15const filteredArr = arr.filter(el => {
16 const duplicate = seen.has(el.id);
17 seen.add(el.id);
18 return !duplicate;
19});
1const arr = [
2 { id: 1, name: "test1" },
3 { id: 2, name: "test2" },
4 { id: 2, name: "test3" },
5 { id: 3, name: "test4" },
6 { id: 4, name: "test5" },
7 { id: 5, name: "test6" },
8 { id: 5, name: "test7" },
9 { id: 6, name: "test8" }
10];
11
12const filteredArr = arr.reduce((acc, current) => {
13 const x = acc.find(item => item.id === current.id);
14 if (!x) {
15 return acc.concat([current]);
16 } else {
17 return acc;
18 }
19}, []);
20
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