I want to create a function that returns multiple objects from array, but it only returns an object from index[1]. Is there a mistake in this for loop? I tried to put the return inside the loop, it returns only one object too.
function Me(arr) {
var person = {}
var year = new Date().getFullYear()
for (var i = 0; i < arr.length; i++) {
console.log(i + 1 + ". " + arr[i][0] + " " + arr[i][1] + " :")
person["firstName"] = arr[i][0]
person["lastName"] = arr[i][1]
person["gender"] = arr[i][2]
person["age"] = arr[i][3]
if (arr[i][3] === undefined) {
person.age = "Invalid Birth Year"
} else {
person.age = year
}
}
return person;
}
console.log(Me([
['Christ', 'Evans', 'Male', 1982],
['Robert', 'Downey', 'Male']
]));
// 1. Christ Evans:
// { firstName: 'Christ',
// lastName: 'Evans',
// gender: 'Male',
// age: 37 }
// 2. Robert Downey:
// { firstName: 'Robert',
// lastName: 'Downey',
// gender: 'Male',
// age: 'Invalid Birth Year' }
console.log(Me([]));
person- with the values you assigned to it last. What do you actually want to do, return multiple objects in an array?