so I got this code that pushes data from an API in an array and it kinda works. If i log the array as a whole it does show the entire array, but if i try to log a specific object within the array it returns "undefined".
my code is below:
let people = [];
fetch('https://randomuser.me/api/?results=10')
.then(function(response){
return response.json();
})
.then(function(data){
for(i=0; i<10; i++){
let person = data.results[i];
let x = {
name: person.name.first + " " +person.name.last,
picture: person.picture.large,
age: person.dob.age,
place: person.location.street + "<br>" + person.location.city + "<br>" + person.location.state
}
people.push(x);
}
}).catch(function(error){
console.log('Data is not shown ' + error.message);
});
console.log(people); // works fine
console.log(people[0]); // returns undefined
console.log(people.length); //returns 0