I want to make an array with country names from a JSON file which contains an array of objects. These objects have a property name with the name of the country, which is what I need.
This is my JavaScript code which returns a list of undefined instead of country names:
import axios from 'axios';
const getTeamsFromUrl = async() => {
const url = 'https://gist.githubusercontent.com/keeguon/2310008/raw/bdc2ce1c1e3f28f9cab5b4393c7549f38361be4e/countries.json';
const response = await axios.get(url);
const listOfCountries = response.data;
// this console.log prints properly the array of objects
// console.log(listOfCountries);
for (let i = 0; i < listOfCountries.length; i++) {
console.log(listOfCountries[i].name);
}
}
console.log(await getTeamsFromUrl());
In the other hand, if I use forEach, I get this error message: listOfCountries.forEach is not a function
let x = listOfCountries.forEach(country => {
console.log(country.name);
});
Thanks in advance!
response.datais not an array so you should check that.xwill always be undefined.