[Solved] How can I sum object values by array data using ES standard? [closed]


A reduce and forEach to group by tag and then taking the values for each tag

var a= [
  {
    "cost": 500,
    "revenue": 800,
    "tag": [
      "new",
      "equipment",
      "wholesale"
    ]
  },
  {
    "cost": 300,
    "revenue": 600,
    "tag": [
      "old",
      "equipment"
    ]
  },
  {
    "cost": 800,
    "revenue": 850,
    "tag": [
      "wholesale"
    ]
  }
]
let x = Object.values(a.reduce((acc,{cost,revenue,tag})=>{
    tag.forEach((t) => {
    if(!acc[t])acc[t]={tag:t,cost:0,revenue:0}
    acc[t].revenue+=revenue
    acc[t].cost+=cost
  })
  return acc;
},{}))
console.log(x)

solved How can I sum object values by array data using ES standard? [closed]