Search code examples
javascriptfetchreact-nativeresponse

Extract data from API


Maybe the question might be too simple but I can't get the data

This is the response of API:

[{"city_id": 33, "city_name": "Nagpur", "country_id": 1, "country_name": "India", "entity_id": 33, "entity_type": "city", "latitude": 21.15, "longitude": 79.09, "title": "Nagpur"}]

this response is being stored inside "a", now when I set state,

this.setState({lat: a})

It shows the same data in the log but when I try to extract latitude and longitude it shows undefined. I'm doing this to extract data

this.setState({lat : a.latitude})  // shows undefined 

Please Help me !!!!!!


Solution

  • It looks like a is array in your response so try:

    this.setState({lat : a[0].latitude})