i have created an object and i am passing an array and different objects to that array , but i cannot access them , it always gives , 0 is undefined or undefined when i want to access element at specific index , also even though there are more then 150 objects inside this array it still gives this message : length: 194 proto: Array(0)
let passglobaldataArraytofun = (countryNames) => {
let objforeachCountryStatistics = {};
objforeachCountryStatistics.countries = [];
for (let index = 0; index < countryNames.length; index++) {
axios
.get(`https://covid19.mathdro.id/api/countries/${countryNames[index]}`)
.then((res) => {
objforeachCountryStatistics.countries.push(res.data);
})
.catch((err) => {
console.log(err);
});
}
let data = objforeachCountryStatistics;
console.log(data);
};
second method
async function passglobaldataArraytofun(countryNames){
let objforeachCountryStatistics = {};
objforeachCountryStatistics.countries = [];
for (let index = 0; index < countryNames.length; index++) {
let res= await axios.get(`https://covid19.mathdro.id/api/countries/${countryNames[index]}`)
objforeachCountryStatistics.countries.push(res.data);
}
console.log(objforeachCountryStatistics)
};