0

I am trying to remove duplicate elements based on an attribute(family) and sum another attribute based on identified duplicate elements.

[{family: "Mobile",total:30},
{family: "Mobile",total:20},
{family: "Mobile",total:50},
{family: "Laptop",total:20}]

i am expecting an array after removing duplicates and adding total as below

[{family: "Mobile",total:100}, 
{family: "Laptop",total:20}]

i tried using below code but it was not calculation total

var resultArray = this.list1.reduce((unique, o) => {
    if(!unique.some(obj => obj.family === o.family )) {
      unique.push(o);
    }
    return unique;
},[]);
console.log(resultArray ); `

Can you help me here
Gajini
  • 413
  • 1
  • 5
  • 21

0 Answers0