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

194
Vistas
How can I group an array?

I have this array :

array=[{x:2,y:test0,z:title},{x:3,y:test0,z:title},{x:4,y:test0,z:title},{x:5,y:test0,z:title2},{x:6,y:test0,z:title2},{x:7,y:test0,z:title2}]

And I turned it into this array this way:

 const groups = array.reduce((acc, row) => {
       acc[row[title]] = acc[row[title]] || [];
        acc[row[title]].push(row);
        return acc;
      }, {});

this is result:

  [
    title:[{x:2,y:test0},{x:3,y:test0},{x:4,y:test0}]
    title2:[{x:5,y:test1},{x:6,y:test1},{x:7,y:test1}]
    ]

And I want to have this array:

[
 title:[test0:[{x:2},{x3:},{x:4}]]
 title2:[test1:[{x:5}],test2:[{x:6}],test3:[{x:7}]]]
]

How can I get to this array?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You want y to be the key. You can just use object destructuring like:

const {y, ...obj} = row[title];
const fixedRow = {[y]: obj};

In this way y gets removed from the object, and you can use the previous value of y as key and the remaining properties as value.

This won't look exactly like your expected result, because arrays can't have properties, but I suppose it's just a typo.

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