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

84
Visualizações
Array of objects to another array sumarized

I'm new to JS and I'm having issues to sum values from one array into another one summarized.

I have this data:

const data = [
  {
    category: 'personal',
    amount: 80000,
    month: 'Diciembre',
  },
  {
    category: 'comida',
    amount: 207000,
    month: 'Diciembre',
  },
  {
    category: 'comida',
    amount: 43000,
    month: 'Diciembre',
  },
  {
    category: 'salidas',
    amount: 124000,
    month: 'Diciembre',
  },
  {
    category: 'casa',
    amount: 450505,
    month: 'Diciembre',
  },
  {
    category: 'casa',
    amount: 500000,
    month: 'Diciembre',
  },
  {
    category: 'varios',
    amount: 260000,
    month: 'Diciembre',
  },
  {
    category: 'casa',
    amount: 296300,
    month: 'Diciembre',
  },
];

And I want to convert it in something like:

const dataSummarized = [
  {
    category: 'personal',
    amount: TOTAL_AMOUNT_PERSONAL_CATEGORY
    month: 'Diciembre',
  },
  {
    category: 'comida',
    amount: TOTAL_AMOUNT_COMIDA_CATEGORY
    month: 'Diciembre',
  },
  {
    category: 'salidas',
    amount: TOTAL_AMOUNT_SALIDAS_CATEGORY,
    month: 'Diciembre',
  },
  {
    category: 'casa',
    amount: TOTAL_AMOUNT_CASA_CATEGORY,
    month: 'Diciembre',
  }
];

I've tried several options, but without results.

Some of the solutions I've tried are posted here Sum javascript object propertyA values with same object propertyB in array of objects

Evidently, I'm missing something because I couldn't make it work :(

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

We can then use Array.reduce() to group items by category, then month (I presume you want expense totals per month).

We create a grouping key based on category and month, then sum the total amount for each of these keys:

const data = [ { category: 'personal', amount: 80000, month: 'Diciembre', }, { category: 'comida', amount: 207000, month: 'Diciembre', }, { category: 'comida', amount: 43000, month: 'Diciembre', }, { category: 'salidas', amount: 124000, month: 'Diciembre', }, { category: 'casa', amount: 450505, month: 'Diciembre', }, { category: 'casa', amount: 500000, month: 'Diciembre', }, { category: 'varios', amount: 260000, month: 'Diciembre', }, { category: 'casa', amount: 296300, month: 'Diciembre', }, ]; 

const result = Object.values(data.reduce((acc, { category, month, amount }) => { 
    const key = `${category}-${month}`;
    acc[key] = acc[key] || { category, month, amount: 0 };
    acc[key].amount += amount;
    return acc;
}, {}));

console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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