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);
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);
1var arrOfObj = [
2 {
3 id:1 ,name:'abc',age:27
4 },
5 {
6 id:2 ,name:'pqr',age:27
7 },
8 {
9 id:1 ,name:'abc',age:27
10 },
11 ]
12
13var setObj = new Set(); // create key value pair from array of array
14
15var result = arrOfObj.reduce((acc,item)=>{
16 if(!setObj.has(item.age)){
17 setObj.add(item.age)
18 acc.push(item)
19 }
20 return acc;
21},[]);//converting back to array from mapobject
22
23console.log(result);