Home > Mobile >  Access to API data when some of the values begins with an underscore?
Access to API data when some of the values begins with an underscore?

Time:09-12

I have the following data from an API Link from data from de api

I need to access into product_last_data but I don't know how to access because of the element '_source' with the underscore at the beginning

I've been trying the following:

response.data.data (This works fine)

But when I try to access to any element with the underscore I don't know how to do it I've been trying this:

response.data.data['_source']

CodePudding user response:

response.data.data is an array of objects, so you need to iterate the array (or pick a single element) to access the objects underneath. For example:

response.data.data.forEach(o => console.log(o._source.product_last_data))

or

console.log(response.data.data[0]._source.product_last_data)

Note that _source is a valid key which does not require [] notation to access.

  • Related