I have a nested array data structure like this -
var arrays = [
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
];
and I need to transform this into an array of objects -
[
{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]..
My code below gives-
function objectArray(arr) {
var obj1 ={};
var empData=[];
for (var i = 0; i < arr.length; i++)
{
if (Array.isArray(arr[i]))
{
arr[i].reduce(function(acc,prd){
// console.log(prd);
console.log(acc.key=prd[0],acc.Value=prd[1]);//--> output shown below
return acc;
},{});
}
}
}
var returnArrayOfObjs = objectArray(arrays);
var empData = [];
empData.push(returnArrayOfObjs);
console.log(empData);
The above log statement gives me an [undefined] as shown below in my output -
The output I get is as below - What am I doing wrong? Pls help!
firstName Joe
lastName Blow
age 42
role clerk
firstName Mary
lastName Jenkins
age 36
role manager
[undefined]
arr[i].reduceanywhere.