I'm attempting to turn the array below into properties for an object, the problem I'm having is properly using nested array notation: array[x][y]
var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
function fromListToObject(array) {
var result = {};
for(var i = 0;i < array.length;i++){
console.log(array[i]); // only here to view output
for(var j = 0;j < array[i].length;j++){
console.log(array[i][j]); // only here to view output
console.log(array[i][j+1]); // only here to view output
result[array[i][j]] = array[i][j+1];
}
}
return result;
}
fromListToObject(array);
The output so far:
[ 'make', 'Ford' ] //inner array
make //should be the key
Ford //should be the value
Ford //where did this come from?
undefined
[ 'model', 'Mustang' ]
model
Mustang
Mustang
undefined
[ 'year', 1964 ]
year
1964
1964
undefined
=> { '1964': undefined, //How in the heck?
make: 'Ford', //<--- actually what I wanted
Ford: undefined,
model: 'Mustang',
Mustang: undefined,
year: 1964 }
I did solve the assignment with forEach:
function fromListToObject(array) {
var result = {};
array.forEach(function(element){
result[element[0]] = element[1];
});
return result;
}
and I can just capture the inner array into a temporary variable as well, so this is for my edification. Any help/hints would be greatly appreciated. Thanks.
EDIT
I just wanted to say thanks again for the answers, us rookies need guidance like yours.
j, so you'll get what used to bej + 1in your next iteration...