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

395
Vistas
Merge objects into array

I want to convert multiple objects with same id as test to array of objects

Actual:

const array= [
    { "test": 1},
    { "test": 2},
 { "test": 3},
 { "test": 4},
]

Expected:

test: [1,2,3,4]

Can someone please help

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

0

Just use the native method map (read more here or here) like this:

const array= [
  { "test": 1},
  { "test": 2},
  { "test": 3},
  { "test": 4},
];
const newArray = array.map(p => p.test);
console.log(JSON.stringify(newArray)); //[1,2,3,4]

Hope this helps.. ;D

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the map function for this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map :

const newarray = array.map(x => x.test);
console.log(newarray);
about 4 years ago · Juan Pablo Isaza Denunciar

0

const result = {};

yourArray.forEach( ( object ) => {
  const keys = Object.keys( object );
  for ( let i = 0; i < keys.length; i++ ) {
     const key = keys[ i ];
     if ( ! key in result ) { 
        result[ key ] = [];
     }
     result[key] = [...result[key], ...object[key] ];
  }
});

console.log( result );

In this case:

  1. You need to iterate in your array
  2. On each iteration you have a new object
  3. From there you iterate in all the keys of that current object
  4. If the key does not exists in your result create a new empty array
  5. Merge the value from the result with the current value of the object.
  6. Values are stored in result.
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