I am trying to access the data from the capitals array in Javascript but I cannot as it gives me undefined when I alert or console.log it. I can see all the data in my console and can't access it using the index. console_screenshot
var capitals = []
countries.forEach(function (item, index) {
fetch("https://restcountries.eu/rest/v2/alpha/"+countries[index])
.then((resp) => resp.json())
.then(function(data){
capitals[index] = data.capital
})
.catch(function(error) {
console.log(error);
});
});
console.log(capitals)
alert(capitals)
//This following line doesn't work because my capital[1] shows as undefined
Document.getElementById("result").innerHTML = capitals[1];
})