I am trying to write a function which takes in an array of arrays, and returns an object with each pair of elements in the array as a key-value pair. I have searched stack overflow and came up with the following code. However, my code below only returns the first array { make: 'Ford' }. My code does not return the rest of the arrays. Any suggestions on why my function does not return the rest of the array of arrays?
var array = [
['make', 'Ford'],
['model', 'Mustang'],
['year', 1964]
];
function fromListToObject() {
for (var i = 0; i < array.length - 1; ++i) {
var object = {};
//creates an object as a variable
var newArray = array[i];
//creates a variable for the array within the array
object[i] = newArray[1];
//sets the object of the element to position 1 of the array within the array
object[newArray[0]] = object[i];
//sets the key of the element to position 0 of the array within the array
delete object[i];
// removes the previous key of 0
return object;
}
}
console.log(fromListToObject(array));