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

102
Visualizações
Add objects of two different Arrays

I have two arrays and each of them has objects. How best can I simplify adding two objects into one but in a new list. e.g

a = [{a:1, b:2, c:3}, {d:1, e:4, f:2}]
b = [{m:1, n:2, o:4}, {r:1,s:3,u:5}, {k:1,j:4,f:8}]

z = [{a:1, b:2, c:3, m:1, n:2, o:4}, {d:1, e:4, f:2, r:1,s:3,u:5}, {k:1,j:4,f:8}]

Suppose you have list a and b, I want to add the objects of each position together in list z.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could merge the two object in this way:

let a = [{a:1, b:2, c:3}, {d:1, e:4, f:2}]
let b = [{m:1, n:2, o:4}, {r:1,s:3,u:5}, {k:1,j:4,f:8}]

let z = [];

b.forEach((x, i) => {
     let merged = {...x, ...a[i]};
     z.push(merged)
})

console.log(z);

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd go over the maximum length of a and b and use Object.assign to copy the values. Assuming you want a generic solution to any two arrays, where either can be longer than the other, note that you need to check the lengths as you go:

z = [];
for (let i = 0; i < Math.max(a.length, b.length); ++i) {
    const result = i < a.length ? a[i] : {};
    Object.assign(result, i < b.length ? b[i] : {});
    z.push(result);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this :

const a = [{a:1, b:2, c:3}, {d:1, e:4, f:2}];
const b = [{m:1, n:2, o:4}, {r:1,s:3,u:5}, {k:1,j:4,f:8}];
let c = [];

if(a.length > b.length){
    c = a.map((e,i) => {
     return {
        ...e,
        ...b[i]
     }
   });
}else{
  c = b.map((e,i) => {
      return {
        ...e,
        ...a[i]
     }
  });
}
console.log(c);

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