I am trying to get a country from a json file i am fetching online. I am trying to create a function to get a country from the dataset as to not have duplicate code, but I am having some trouble. The console.log in the get country function displays the object correctly but in the gettooltip function it is undefined.
Can anyone help explain why this is the case? I have tried using the ... operator and the one shown below.
function getCountryByISO3(iso3){
data.forEach(country => {
if (country.countryInfo.iso3 === iso3){
let r = Object.assign({}, country)
console.log(r)
return r
}
})
}
let datapoint = 'cases'
//returns string for tooltip
function getTooltip(iso3){
//if data hasnt loaded
if(data != null) {
try {
console.log(getCountryByISO3(iso3))
} catch (e){
return 'unknown'
}
}
}