简体   繁体   English

如何在React Native上访问数组状态或JSON数据?

[英]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 this.state.data.index

but not getting index value. 但没有获得索引值。 How to access index? 如何访问索引?

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: 您的this.state.data是一个数组,因此您需要遍历该数组以获取索引值,并使用它访问具有index属性的所有对象,例如:

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. 或者你需要明确定义的索引值this.state.data[0].index获得index在索引对象的值0等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM