【问题标题】:How to access value 0 of json data (react-native) expo如何访问 json 数据(react-native)expo 的值 0
【发布时间】:2020-06-09 08:50:17
【问题描述】:

我有一些 json 数据,例如:

[{"timestamp":"2020-03-23T14:00:00.000Z","symbol":"AAL","name":"American Airlines Group","industry":"Industrials","open":10.9,"high":11.36,"low":10.01,"close":10.25,"volumes":55494100},

{"timestamp":"2020-03-22T14:00:00.000Z","symbol":"AAL","name":"American Airlines Group","industry":"Industrials","open":10.6526,"high":12,"low":10.3,"close":10.38,"volumes":62681800},


{"timestamp":"2020-03-19T14:00:00.000Z","symbol":"AAL","name":"American Airlines Group","industry":"Industrials","open":11.6,"high":12.16,"low":10.01,"close":10.29,"volumes":71584500},{"timestamp":"2020-03-18T14:00:00.000Z","symbol":"AAL","name":"American Airlines 

我只想获取 0 的索引值:

This one:   [{"timestamp":"2020-03-23T14:00:00.000Z","symbol":"AAL","name":"American Airlines Group","industry":"Industrials","open":10.9,"high":11.36,"low":10.01,"close":10.25,"volumes":55494100},{"timestamp":"2020-03-22T14:00:00.000Z",

我的代码:

  let movies = state.myListData.map((val) => {
    return (
      <View key={val.symbol} style={styles.text}>
        <Text style={styles.text} key={val.symbol}>
          {val.high}
        </Text>
        <Text style={styles.text} key={val.symbol}>
          {val.close}
        </Text>
      </View>
    );
  });

现在我得到了所有的值,但我怎样才能只得到值 0?有什么办法吗?

【问题讨论】:

  • 使用Array.splice()方法
  • 你能告诉我在我的代码中哪里写吗?我不明白

标签: json reactjs react-native expo


【解决方案1】:

你可以这样做:

let item = state.myListData[0];
let movie = (
  <View key={item.symbol} style={styles.text}>
    <Text style={styles.text} key={item.symbol}>
      {item.high}
    </Text>
    <Text style={styles.text} key={item.symbol}>
      {item.close}
    </Text>
  </View>
)

【讨论】:

  • 如果我写这个,它会说它的 undefined let item = state.myListData[0];
  • 但是如果我打印 state.myListData 它会打印所有的值。如果有帮助的话,那就是我的 json 数据:Object { "close": 63.37, "high": 67.5599, "industry": "Health Care", "low": 62.09, "name": "Agilent Technologies Inc", "open": 66.8, "symbol": "A", "timestamp": "2020-03-23T14:00:00.000Z", "volumes": 2989560, } Object { "close": 63.37, "high": 67.5599, "industry": "Health Care", "low": 62.09, "name": "Agilent Technologies Inc", "open": 66.8, "symbol": "A", "timestamp": "2020-03-23T14:00:00.000Z", "volumes": 2989560, }
猜你喜欢
  • 2017-07-09
  • 2021-07-12
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 2019-06-11
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多