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

244
Visualizações
How to add different values to an array of object in Javascript?

I have an array of objects like this:

"data":
[
{"name":"Jane Doe",
"age":28,
"title":"Manager"
},
{"name":"James Brown",
"age":20,
"title":"Intern"
},
{"name":"Irene Vargas",
"age":35,
"title":"Accountant"
},
]

and I have a separate array of objects:

[
{"experience": "7 years",
"hobby": "Music"
},
{"experience": "6 months",
"hobby": "Video games"
},
{"experience": "15 years",
"hobby": "Skydiving"
},
]

How do I add values from 2nd array to the first one, result should be like this:

"data":
[
{"name":"Jane Doe",
"age":28,
"title":"Manager",
"experience": "7 years",
"hobby": "Music"
},
{"name":"James Brown",
"age":20,
"title":"Intern",
"experience": "6 months",
"hobby": "Video games"
},
{"name":"Irene Vargas",
"age":35,
"title":"Accountant",
"experience": "15 years",
"hobby": "Skydiving"
},
]

I can't figure out how to add values from the 2nd array separately to the 1st array, any help and advise is greatly appreciated.

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

0

Loop through arr1 and use Object.assign to assign the properties of the item in the same index in the other array to the current item:

const arr1=[{name:"Jane Doe",age:28,title:"Manager"},{name:"James Brown",age:20,title:"Intern"},{name:"Irene Vargas",age:35,title:"Accountant"}],arr2=[{experience:"7 years",hobby:"Music"},{experience:"6 months",hobby:"Video games"},{experience:"15 years",hobby:"Skydiving"}];

arr1.forEach((e,i) => Object.assign(e, arr2[i]))

console.log(arr1)

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