Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How can I sum object values by array data using ES standard?

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'                     ]} 
  ] 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda