JS newbie here. I'm learning to convert a nested array into an array containing objects.
My question: Why does var outObj = {}; turns into an array after pushing outobj{} into outArr[]?
(Please see expected and actual output at the end of this post) Thank you in advance for your help.
function transformEmployeeData(inArr) {
var outArr = [];
for(var x = 0; x < inArr.length; x++){
var outObj = {}; // The obj that will be stores in outArr[]
var a1 = inArr[x];
//console.log(a1 + '\n');
for(var y = 0; y < a1.length; y++){
outObj[a1[y][0]] = a1[y][1];
//console.log(outObj[a1[y][0]]);
}
outArr.push(outObj);
}
return arr;
}
var arr = [
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
];
console.log(transformEmployeeData(arr));
/*
Expected Output:
[
{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]
Actual Output:
[ [ [ 'firstName', 'Joe' ],
[ 'lastName', 'Blow' ],
[ 'age', 42 ],
[ 'role', 'clerk' ] ],
[ [ 'firstName', 'Mary' ],
[ 'lastName', 'Jenkins' ],
[ 'age', 36 ],
[ 'role', 'manager' ] ] ]
*/
console.log(transformEmployeeData([]));and be surprised ;)