Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda