Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!