-4

I have an array of objects. Each object has a "tag" property with an array list. The values in this list repeat from object to object. I would like to sum up "cost" and "revenue" based on the "tag" arrays. Not sure explanation makes sense but I have the data structure below.

What I want the data to look like:

const expected = 
  [ { cost:  500, revenue:  800, tag: 'new'       } 
  , { cost:  800, revenue: 1400, tag: 'equipment' } 
  , { cost: 1300, revenue: 1650, tag: 'wholesale' } 
  , { cost:  300, revenue:  600, tag: 'old'       } 
  ] 

what the data looks like:

const data = 
  [ { cost: 500, revenue: 800, tag: [ 'new', 'equipment', 'wholesale' ]} 
  , { cost: 300, revenue: 600, tag: [ 'old', 'equipment'              ]} 
  , { cost: 800, revenue: 850, tag: [ 'wholesale'                     ]} 
  ] 

1 Answers1

1

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)
cmgchess
  • 7,996
  • 37
  • 44
  • 62