Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda