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

145
Visualizações
seeking a concise way to accumulate values

The following code produces the desired output but is there a more concise way to write it? ie. one liner approaches

const data = [
    { id: '385/72/21', month: 4 },
    { id: '385/72/21', month: 7 },
    { id: '385/72/21', month: 8 },
    { id: '385/72/21', month: 10 },
    { id: '461/80/07', month: 1 },
    { id: '461/80/07', month: 5 },
    { id: '461/80/07', month: 9 }
];

const accumulator = Object.assign({}, ...data.map(o => ({ [o.id]: [] })))
data.forEach(o => accumulator[o.id].push(o.month));
console.log(accumulator);

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

0

const data = [
    { id: '385/72/21', month: 4 },
    { id: '385/72/21', month: 7 },
    { id: '385/72/21', month: 8 },
    { id: '385/72/21', month: 10 },
    { id: '461/80/07', month: 1 },
    { id: '461/80/07', month: 5 },
    { id: '461/80/07', month: 9 }
];

const result = data.reduce((accu, curr) => {
    accu[curr.id] = Array.isArray(accu[curr.id]) ? [...accu[curr.id], curr.month] : [curr.month];
    return accu;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I prefer using the comma operator instead of spreading over and over again (depending on your dataset you may incur a performance penalty):

console.log(

  data.reduce((acc, {id, month}) =>
    (acc[id] ??= [], acc[id].push(month), acc), {})

);
<script>
const data = [
    { id: '385/72/21', month: 4 },
    { id: '385/72/21', month: 7 },
    { id: '385/72/21', month: 8 },
    { id: '385/72/21', month: 10 },
    { id: '461/80/07', month: 1 },
    { id: '461/80/07', month: 5 },
    { id: '461/80/07', month: 9 }
];
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Another way of using Array.reduce() to get the required output:

const data = [ { id: '385/72/21', month: 4 }, { id: '385/72/21', month: 7 }, { id: '385/72/21', month: 8 }, { id: '385/72/21', month: 10 }, { id: '461/80/07', month: 1 }, { id: '461/80/07', month: 5 }, { id: '461/80/07', month: 9 } ];
const result = data.reduce((acc, { id, month}) => ({ ...acc, [id]: [ ...(acc[id] || []), month ]  }), {})

console.log(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