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

156
Visualizações
How to zip two arrays object in Javascript

I have two arrays object:

const arr1 = [{a: 'QQQ'}, {b: 'WWW'}]
const arr2 = [{a: 'EEE', b: 'RRR'}, {a: 'TTT', b: 'YYY'}]

Then I want the resulting array to be like this:

const results = [{'QQQ': 'EEE', 'WWW': 'RRR'}, {'QQQ': 'TTT', 'WWW': 'YYY'}]

Any way to do this? Thank you very much!

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

0

Not sure if this is the most efficient way to do this, but it works for the example provided. This method relies on some relatively newer features of JavaScript, but ES6 is widely supported, so it hopefully won't be an issue in your case.

First, isolate the values in arr1 to be used as the object properties for the final result.

Then, re-map the objects of the second array by extracting the values from each object using Object.values() and reduce that to an object with the property names from the first array.

var arr1 = [{a: 'QQQ'}, {b: 'WWW'}];
var arr2 = [{a: 'EEE', b: 'RRR'}, {a: 'TTT', b: 'YYY'}];

var keys = arr1.reduce((valueArray,obj) => [...valueArray, ...Object.values(obj)],[]);

var results = arr2.map(o => Object.values(o).reduce((newObj,v,i) => ({...newObj,[keys[i]]:v}),{}),[])

console.log(results);

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