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

106
Vistas
Merge two arrays of objects with the same keys in JavaScript

I would like to merge these two arrays

const arr1 = [{ label: 'one', value: 'one', type: 'arr1'}];
const arr2 = [{ label: 'two', value: 'two', type: 'arr1'}];

to get the following result

const arr3 = [{ label: 'one', value: 'one' type: 'arr1'}, { label: 'two', value: 'two' type: 'arr1'}];

I've tried arr1.concat(arr2) and _.merge(arr1, arr2) and [arr1, arr2] and get the following error

TypeError: Invalid attempt to spread non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your third try [arr1, arr2] was really close to a right answer, you can use the new spread syntax (...) to concat the arrays by doing to following:

const arr1 = [{ label: "one", value: "one", type: "arr1" }];
const arr2 = [{ label: "two", value: "two", type: "arr1" }];

console.log([...arr1, ...arr2]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try this code it works fine :

 const arr1 = [{ label: 'one', value: 'one', type: 'arr1'}];
        const arr2 = [{ label: 'two', value: 'two', type: 'arr1'}];
        const arr3 = arr1.concat(arr2);
        
        console.log(arr3);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You missed comma before type

But you can solve it that way.

const arr1 = [{ label: 'one', value: 'one', type: 'arr1'}];
const arr2 = [{ label: 'two', value: 'two', type: 'arr1'}];

const arr3 = arr1.concat(arr2);

console.log(arr3);

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