0

Please excuse my first post. So I got a RapidAPI for Covid-19 updates set up and properly spouting it's results to the console as an array JSON as the following: YAML Array Output Now, I want to be able to list specific countries and their following info from the JSON array using fetch and inner HTML method from the JSON array above. So, I tried to use

document.getElementById('countryname').innerHTML = response.0.country; which returns an error. But each country is defined under their own tables using numbers 0 to 232.

So I tried document.getElementById('countryname').innerHTML = response.country[0]; but it returns as "Cannot read property '0' of undefined." I looked everywhere for the solution and I'm stumped. response.country by itself also turns up as undefined.

2 Answers2

0

It's

response[0].country

or even:

response[0]['country']

I suggest you read the Array documentation to understand how they work

gbalduzzi
  • 9,356
  • 28
  • 58
0

looks like response is an Array so you can index each individual element of this array with reponse[index] so for the first element reponse[0]

each response item has a "country" property then so you should be able to access this as reponse[0].country

lwohlhart
  • 1,829
  • 12
  • 20
  • Using reponse[0].country; returns "cannot read property 'country' of undefined" and reponse[0].country; returns "response is not defined" – user15237903 Mar 02 '21 at 14:37
  • could you post the code how you retrieve the api response? If your `response` object is not defined my first guess would be that the API call is asynchronous and you have to wait for it to be loaded before accessing the response. Can't tell without the rest of the surrounding code. – lwohlhart Mar 02 '21 at 16:20