remove object duplicates from list javascript code example
Example 1: how to remove duplicate array object in javascript
let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
function removeDuplicates(data, key) {
return [
...new Map(data.map(item => [key(item), item])).values()
]
};
console.log(removeDuplicates(person, item => item.name));
Example 2: filter duplicate value in array of object typescript
var arrOfObj = [
{
id:1 ,name:'abc',age:27
},
{
id:2 ,name:'pqr',age:27
},
{
id:1 ,name:'abc',age:27
},
]
var setObj = new Set();
var result = arrOfObj.reduce((acc,item)=>{
if(!setObj.has(item.age)){
setObj.add(item.age)
acc.push(item)
}
return acc;
},[]);
console.log(result);