I have an array 1:
['a', 'b', 'c']
And array 2:
[{ a: 1, b: 2, c: 3, d: 5 }]
How to combine 2 arrays as below:
[{a: 1, b: 2: c: 3}]
The result only includes the items in first array. I want an optimal solution.
I have an array 1:
['a', 'b', 'c']
And array 2:
[{ a: 1, b: 2, c: 3, d: 5 }]
How to combine 2 arrays as below:
[{a: 1, b: 2: c: 3}]
The result only includes the items in first array. I want an optimal solution.
A solution using reduce
a1 = ['a', 'b', 'c', 'e'];
a2 = [{ a: 1, b: 2, c: 4, d: 5 }];
a = a1.reduce((a,key) => [... a, {[key]: a2[0][key]}], []);
console.log(a);
In case a key from the first array is missing in the second array, its value will be set as undefined.