【问题标题】:React Native - Access specific Key-Value pairReact Native - 访问特定的键值对
【发布时间】:2021-07-04 15:40:40
【问题描述】:

我正在尝试控制台记录特定键的特定值,在这种情况下我只想控制台记录数字 54。

import React, {Component} from 'react';
import {Text, View} from 'react-native';

export default class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      data: [],
      isLoading: true,
    };
  }

  componentDidMount() {
    const that = this;
    fetch('http://10.0.2.2:8085/lastRoutineID/')
      .then(function (response) {
        return response.json();
      })
      // .then(function (jsonData) {
      //   return JSON.stringify(jsonData);
      // })
      .then(function (jsonStr) {
        that.setState({data: jsonStr});
        console.log('mount', jsonStr[0]);
      });
  }

  render() {
    console.log('json data ' + this.state.data);
    return (
      <View style={{flex: 1, padding: 24}}>
        <Text>hi</Text>
      </View>
    );
  }
}


[
   {
     lastIdRutina: 54
   }
]

这是我在终端中得到的,我只想控制台记录这个数字

[Thu Apr 08 2021 11:54:27.566]  LOG      json data [object Object]
[Thu Apr 08 2021 11:54:27.574]  LOG      mount {"lastIdRutina": 54}

【问题讨论】:

    标签: javascript node.js reactjs react-native api


    【解决方案1】:

    要访问该值,您可以使用. 运算符或下标[]

    console.log('mount', jsonStr[0].lastIdRutina)
    // or
    console.log('mount', jsonStr[0]['lastIdRutina'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 2017-02-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多