typescript remove duplicates from array based on property code example
Example 1: 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);
Example 2: remove duplicate objects based on id from array angular 8
function getUnique(arr, comp) {
const unique = arr.map(e => e[comp])
.map((e, i, final) => final.indexOf(e) === i && i)
.filter((e) => arr[e]).map(e => arr[e]);
return unique;
}
console.log(getUnique(arr,'id'));