I have an array here:
var array = [
[
[\'firstName\', \'Nork\'], [\'lastName\', \'James\'], [\'age\', 22], [\'position\', \'writer\']
],
[
Use [].map over [].reduce
The
map()
method creates a new array with the results of calling a provided function on every element in this array.
Thereduce()
method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
var array = [
[
['firstName', 'Nork'],
['lastName', 'James'],
['age', 22],
['position', 'writer']
],
[
['firstName', 'James'],
['lastName', 'Rodel'],
['age', 25],
['position', 'programmer']
]
];
function mergeObjectArray(array) {
return array.map(function(el) {
return el.reduce(function(a, b) {
a[b[0]] = b[1];
return a;
}, {})
});
}
console.log(mergeObjectArray(array));