This particular challenge calls for me to convert an array of arrays (is there a term for this?) into an array of objects, like so:
[[['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']],
[['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']]]
into:
[{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}]
Here's my code, so far:
function transformEmployeeData(array) {
var object={};
for (var i = 0; i < array.length;i++){
var newArray = array[i];
object[i] = newArray[1];
object[newArray[0]] = object[i];
newArray.push(object);
delete object[i];
}
return object;
}
var myArray= [['firstName','Joe'],['lastName','Blow'],['age', 42], ['role','clerk']];
transformEmployeeData(myArray);
It seems to work great, until I add another nested array with a second (or third, or fourth, which is the whole point of the thing) employee's data.
I'm sure it has something to do with the loop, but I can't spot it.
Help appreciated!
newArrayand sets the values from the even indexes to the props from the odd indexes. Btw,object[i] = newArray[1]- this line is redundant.