I am parsing an array of JSON objects but cannot for the life of me figure out why I cannot access an individual object within the array. I can log the the entire array but when I attempt to access a specific object within the array it returns undefined.
function fetchContacts(i) {
var request = new XMLHttpRequest();
request.open("GET", contactURL[i]);
request.onload = function() {
info = JSON.parse(request.responseText);
contactData[i] = info;
};
request.send();
};
btn.addEventListener("click", function() {
for (i = 0; i < contactURL.length; i++) {
fetchContacts(i);
}
console.log(contactData[0]); // returns undefined
console.log(contactData) // returns JSON objects in an array
});