1const arr = [{id: 1, name: 'one'}, {id: 2, name: 'two'}, {id: 1, name: 'one'}]
2
3const ids = arr.map(o => o.id)
4const filtered = arr.filter(({id}, index) => !ids.includes(id, index + 1))
5
6console.log(filtered)
1// 1. filter()
2function removeDuplicates(array) {
3 return array.filter((a, b) => array.indexOf(a) === b)
4};
5// 2. forEach()
6function removeDuplicates(array) {
7 let x = {};
8 array.forEach(function(i) {
9 if(!x[i]) {
10 x[i] = true
11 }
12 })
13 return Object.keys(x)
14};
15// 3. Set
16function removeDuplicates(array) {
17 array.splice(0, array.length, ...(new Set(array)))
18};
19// 4. map()
20function removeDuplicates(array) {
21 let a = []
22 array.map(x =>
23 if(!a.includes(x) {
24 a.push(x)
25 })
26 return a
27};
28/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"
1//check one attribute
2let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
3
4person = person.filter((item, index, self) =>
5 index === self.findIndex((t) => (
6 t.name === item.name
7 ))
8)
9
10//check two attributes
11let person = [{place: "uno", name: "john"}, {place: "duno", name: "jane"}, {place: "duno" ,name: "imelda"}, {place: "uno" ,name: "john"}];
12
13person = person.filter((item, index, self) =>
14 index === self.findIndex((t) => (
15 t.place === item.place && t.name === item.name
16 ))
17)
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 data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
10console.log(data);