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

122
Vistas
how to create array of object in javascript group by specific field?

I have a data like this:

const input = [ {title: 'head', value: a, amount: 10},
                {title: 'head', value: b, amount: 5},
                {title: 'body', value: c, amount: 20} ];

I want to make an array of object literals with data like this:

const output = [{title: 'head',
                 values: [{value: a, amount: 10},
                          {value: b, amount: 5}]
                },
                {title: 'body',
                 values: [{value: a, amount: 20}]
                }];

since I'm not used to javascript, I'm having trouble wrapping values by title. 🥲

plz help me.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use Set to get the unique values and then map matching entries to the result like this:

const out = [...new Set(input.map(i=>i.title))].map(e=>{
    return {
        title: e,
        values: input.filter(p=>p.title === e).map(p=>{
            return {
                value: p.value,
                amount: p.amount
            };
        })
    };
});
about 4 years ago · Santiago Trujillo 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