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

155
Visualizações
Accumulate values in array without reducing into single value

I want to do something really similar to reduce with values in my array, but I do not want to sum it all up into one value. I want to carry on the summed up value and insert the increment into each consecutive value in the array.

Like this:

const array = [{ number: 6, value: 10 }, { number: 5, value: 12 }, { number: 2, value: 5 }]

// Do something like this
const newArray = array.reduce((accumulated, item) => {
  return {  
    number: (100 / item.number) + accumulated.value || 0,
    value: ((100 / item.number) + accumulated.value || 0) * item.value
  }
}, 0)

// But the result should be something like this
newArray = [{ number: 6, value: 10 }, { number: 30, value: 360 }, { ...and so on }]

How can this be achieved? ES6 with/without lodash preferred.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Since the input and output items are one-to-one, I think using .map would be more intuitive than .reduce - you can keep an outside variable for the last object returned.

const array = [{ number: 6, value: 10 }, { number: 5, value: 12 }, { number: 2, value: 5 }]

let lastReturn = array.shift();
const newArray = [lastReturn].concat(array.map(({ number, value }) => {
  const newObj = {  
    number: (100 / number) + lastReturn.value || 0,
    value: ((100 / number) + lastReturn.value || 0) * value
  };
  lastReturn = newObj;
  return newObj;
}));
console.log(newArray);

While it'd be technically possible to use .reduce for this (you can use .reduce to achieve anything related to array iteration, .map makes more sense here.

about 4 years ago · Santiago Gelvez 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