I am attempting to change an array of arrays containing two value arrays into an array of two objects.
I'm fairly new to JS and I'm not sure where my code is wrong. The array being logged to the console contains the same two objects.
let myArray = [
[
['name', 'John'],
['job', 'Janitor']
],
[
['name', 'Rob'],
['job', 'CEO']
]
];
function transformEmployeeData(array) {
let arrayOfObjects = [];
let newObj = {};
// iterates through employees
for (let i = 0; i < array.length; i++) {
// resets obj to empty
newObj = {};
// iterates through employees properties
for (let j = 0; j < array[i].length; j++) {
newObj[array[j][j][0]] = array[j][j][1];
}
// pushes object into array
arrayOfObjects.push(newObj);
}
return arrayOfObjects;
}
console.log(transformEmployeeData(myArray));