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

113
Vistas
Merge objects from two arrays of object based in the index

i searched a lot about this but i do not find anything that can enlight me about my issue:

I have this code:

let array1 = ["a", "b", 3, {
    p1: 'hola'
  }, "c", "d"],
  array2 = [1, 2, {
    p1: 'adios'
  }],
  result = [],
  i, l = Math.min(array1.length, array2.length);


for (i = 0; i < l; i++) {
  if (typeof array1[i] === 'object' && typeof array2[i] === 'object') {
    result.push(array2[i], ...(JSON.stringify() === JSON.stringify() ?
      [] :
      [array1[i]]
    ));
  } else {
    result.push(array2[i], array1[i]);
  }

}
result.push(...array1.slice(l), ...array2.slice(l));

console.log(result);

i have modified the code with the suggestions, right now the code does this:

we have two array;

array1 = ["a", "b", 3, {p1: 'hello'},"c", "d"] array2 = [1, 2, {p1: 'hello'}]

the result right now is this based in the code:

result: [1, 'a', 2, 'b', {p1: 'hello'}, 3, {p1: 'hello'}, 'c', 'd']

this work fine because i dont want to omit objects that are in different index between the two array, the problem right now is that when the objects in the two array are in the same index this code;

array1 = ["a", "b", {p2: 'goodbye'},"c", "d"] array2 = [1, 2, {p1: 'hello'}]

result : [1, 'a', 2, 'b', {p1: 'hello'}, 'c', 'd']

This is my issue right now, what i want is that when there are object in the same index on two array compare the properties of the objects and is the same skip the first array object and pass the second to the final array, but if the properties are not the same, combine the properties of the object in one, this is the ideal result that i want:

array1 = ["a", "b", {p2: 'goodbye'},"c", "d"] array2 = [1, 2, {p1: 'hello'}]

result : [1, 'a', 2, 'b', {p1: 'hello', p2: 'goodbye'}, 'c', 'd']

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

0

You could compare the items and if same omit the second item.

let array1 = ["a", "b", {p1: 'hello world'},"c", "d"],
    array2 = [1, 2,  {p1: 'hello world'}],
    result = [],
    i, l = Math.min(array1.length, array2.length);
    
for (i = 0; i < l; i++) {
    result.push(array2[i], ...(JSON.stringify() === JSON.stringify()
        ? []
        : [array1[i]]
    ));
}
result.push(...array1.slice(l), ...array2.slice(l));

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think this is what you're after.

let array1 = ['a', 'b', { p2: 'goodbye' }, 'c', 'd'];
let array2 = [1, 2, { p1: 'hello' }];

let result = [];
for (let i = 0; i < Math.max(array1.length, array2.length); i++) {
  if (typeof array1[i] == 'object' && typeof array2[i] == 'object') {
    result.push({ ...array2[i], ...array1[i] });
  } else {
    array2[i] && result.push(array2[i]);
    array1[i] && result.push(array1[i]);
  }
}

console.log(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