0

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: 108, Name: "Coal"}
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142

1 Answers1

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" }
 ]
Adeel Imran
  • 13,166
  • 8
  • 62
  • 77