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

137
Visualizações
Sum of same objects with Array.reduce in Javascript

I have an array like this:

const inputArray = [
  {
    userId: 1,
    sum: 30,
  },
  {
    userId: 1,
    sum: 20,
  },
  {
    userId: 2,
    sum: 50,
  },
  {
    userId: 2,
    sum: 80,
  },
];

Then I wrote a function that sums the values ​​by key and got the following result:

const output = [
  {
    userId: 1,
    sum: 50,
  },
  50,
  {
    userId: 2,
    sum: 130,
  },
  130,
];

How can this error be corrected? Function code below:

const output = inputArray.reduce((accumulator, currentItem) => {
  const index = accumulator.findIndex((item) => item.userId === currentItem.userId);

  if (index < 0) {
    return [...accumulator, currentItem];
  } else {
    return [...accumulator, (accumulator[index].sum += currentItem.sum)];
  }
}, []);

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

0

I would suggest, instead of finding index as it can be costly iterating array again and again, you can simply make an object with userId as key and sum as value.

After that, u can do Object.entries(inputObj) to make and array of object out of this.

var inputArray = [{ userId: 1,sum: 30},{userId: 1,sum: 20},{userId: 2,sum: 50},{userId: 2,sum: 80}];

var outputObj = inputArray.reduce((finalObj, curObj) => {
  if (!finalObj[curObj.userId]) finalObj[curObj.userId] = curObj.sum  //<--- adding new user if do not exist in the object
  else finalObj[curObj.userId] += curObj.sum; // <---- if already present, the add the sum
  return finalObj;
}, {})

var output = Object.entries(outputObj).map((obj) => ({
  userId: obj[0], //<--- key of object as userId
  sum: obj[1]  //<--- value of object as sum
}));

console.log(output)

about 4 years ago · Juan Pablo Isaza Relatório

0

Try

const inputArray = [
  {
    userId: 1,
    sum: 30,
  },
  {
    userId: 1,
    sum: 20,
  },
  {
    userId: 2,
    sum: 50,
  },
  {
    userId: 2,
    sum: 80,
  },
];

const output = inputArray.reduce((accumulator, currentItem) => {
  const matchedItem = accumulator.find((item) => item.userId === currentItem.userId);

  if (!matchedItem) {
    return [...accumulator, currentItem];
  } else {
    matchedItem.sum += currentItem.sum
    return accumulator
  }
}, []);

console.log(output)

you were appending a new item to accumulator even when the item already existed, you just needed to mutate it. Also, since you dealing with an array of objects, you can use find as it will return a reference to the matched object itself, letting you update the object directly, rather than having to use findIndex then use the index to find it in the array again

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this:

const inputArray = [
  {
    userId: 1,
    sum: 30,
  },
  {
    userId: 1,
    sum: 20,
  },
  {
    userId: 2,
    sum: 50,
  },
  {
    userId: 2,
    sum: 80,
  },
];

const res = Array.from(inputArray.reduce((m, {userId, sum}) => m.set(userId, (m.get(userId) || 0) + sum), new Map), ([userId, sum]) => ({userId, sum}));
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