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

99
Vistas
How to loop an array of objects without index as field in Javascript?

I have to arrange a list of orders, then after have the data ready, this list of objects need to be inserted in another object.

I created a simplified code for better comprehension:

const orderList = [];

orderList.push({
    order1: { desc: "smt1" }
})

orderList.push({
    order2: { desc: "smt2" }
})

const result = {
    anotherVar1: 1,
    anotherVar2: 2,
    ...orderList
}
console.log("result", result)

Output
{
    '0': { order1: { desc: 'smt1' } },
    '1': { order2: { desc: 'smt2' } },
    anotherVar1: 1,
    anotherVar2: 2,
}
Desired output:
{
    order1: { desc: 'smt1' },
    order2: { desc: 'smt2' },
    anotherVar1: 1,
    anotherVar2: 2,
}

How to achieve the desired output?

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

0

You want orderList to be an object, not an array. Then instead of using push, you want to set the key/value on the object.

const orderList = {};

orderList.order1 = { desc: "smt1" };
orderList.order2 = { desc: "smt2" };

const result = {
    anotherVar1: 1,
    anotherVar2: 2,
    ...orderList
}

console.log('result', result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

As @Rocket Hazmat mentions in his answer, use an object instead off an array


To answer the original question, you'll need to convert the array of object to an object before you can 'merge' them

const orderList = [];

orderList.push({ order1: { desc: "smt1" } });
orderList.push({ order2: { desc: "smt2" } });

const result = {
    anotherVar1: 1,
    anotherVar2: 2,
    ...Object.assign({}, ...orderList)
};

console.log('result', result);

How to convert an array of objects to object with key value pairs

about 4 years ago · Juan Pablo Isaza Denunciar

0

As a better method keep that array as an object so that you can avoid duplication and additional looping

And as in your same method, you can follow this code to make your desired output

const orderList = [];

orderList.push({
    order1: { desc: "smt1" }
})

orderList.push({
    order2: { desc: "smt2" }
})

const result = {
    anotherVar1: 1,
    anotherVar2: 2,
}
orderList.map(d => {
  for(key in d) {
    result[key] = d[key]
  }
  
})
console.log("result", 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