[Solved] How to zip two arrays object in Javascript [closed]


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);

2

solved How to zip two arrays object in Javascript [closed]