I'm trying to take a nested array and convert it into an object. I'm not understanding why i get undefined
when I try to print the complex array.
My goal is to turn this:
var input = [
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
];
Into this:
var result = [
{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]
So far I have this. I know I'm going to need a nested loop but I can't get my console to display anything past undefined
.
function transformEmployeeData(employeeData) {
var output = {};
var per = {};
// console.log(per);
// console.log(employeeData);
console.log("ED: " + employeeData[0] + "End"); //Expecting the first array in input. Getting output: ED: End
const obj = Object.assign({}, ...employeeData);
console.log("OBJ: " + obj);//Output: OBJ: [object Object]
for (let i = 0; i < employeeData.length; i++) { //Haven't been able to get here because everything is undefined
// console.log(employeeData[i][0] + " " + employeeData[i][1]);
// per[employeeData[i][0][0]] = per[employeeData[i][0][0]] || {};
// per[employeeData[i][0][0]] = employeeData[i][0][1];
}
// console.log("Output: " + namesObj);
}
I've been experimenting with this for an hour. I'm sure its some type of syntax error but I just don't see it.
Even a simple test of a small nested array is giving me an error.
var tests = [["first", "Joe"]["Tests", "two"]];
console.log(tests[0][0]);//TypeError: Cannot read property '0' of undefined