1let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
2
3function removeDuplicates(data, key) {
4
5 return [
6 ...new Map(data.map(item => [key(item), item])).values()
7 ]
8
9};
10
11console.log(removeDuplicates(person, item => item.name));
1let person = [
2{name: "john"},
3{name: "jane"},
4{name: "imelda"},
5{name: "john"},
6{name: "jane"}
7];
8
9const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
10console.log(data);
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}, []);
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});