Shibin Raju Mathew
Shibin Raju Mathew

Reputation: 930

How to access array state or json data on react native?

I have state with

data: [
  { 'keya': 'Quando analisa','index':0 },
  { 'keyb': 'Modelo de texto','index':1 },
  { 'keyc': 'Parte sofreu alteraes','index':2 },
  { 'keyd': 'Todos os geradores','index':3 },
],

Want to access index. Tried

this.state.data.index

but not getting index value. How to access index?

Upvotes: 1

Views: 4127

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Your this.state.data is a array so you either need to loop through the array to get the index value and use it to access all the objects with index property like:

for(var i=0; i<this.state.data.length; i++){
  console.log(this.state.data[i].index);
}

Or you need to explicitly define the index value as this.state.data[0].index to get the index value of object at index 0 and so on.

Upvotes: 2

Related Questions