How To Remove Duplicate Of Arrays In An Object?
How can I remove duplicate of arrays in an object? The object with arrays look like follows: 0:{Id: 185, Name: 'Biomass'} 1:{Id: 125, Name: 'CO2'} 2:{Id: 108, Name: 'Coal'} 3:{Id:
Solution 1:
Suppose your array is
const myArray = [
{Id: 185, Name: "Biomass"},
{Id: 125, Name: "CO2"},
{Id: 108, Name: "Coal"},
{Id: 108, Name: "Coal"},
]
let filtered = myArray.reduce((accumulator, current) => {
if (! accumulator.find(({Id}) => Id === current.Id)) {
accumulator.push(current);
}
return accumulator;
}, []);
filtered
will print
[
{ "Id": 185, "Name": "Biomass" },
{ "Id": 125, "Name": "CO2" },
{ "Id": 108, "Name": "Coal" }
]
Post a Comment for "How To Remove Duplicate Of Arrays In An Object?"