Arrays 我无法从对象获取值。获取未定义对象错误

Arrays 我无法从对象获取值。获取未定义对象错误,arrays,json,react-native,object,react-native-android,Arrays,Json,React Native,Object,React Native Android,我不熟悉React Native,我想获取对象值并将其打印在文本中,但我发现undefined不是一个对象(评估json['label']) 格式如下: [{“id”:“1”,“label”:“abc”,“icon”:{“uri”:“imagepath”}] 所以现在我要存储它,并尝试将标签值保存在变量中 for(let i = 0; i < 4; i++){ const json=this.state.typeofCountry[i]; const originallabl

我不熟悉React Native,我想获取对象值并将其打印在文本中,但我发现undefined不是一个对象(评估json['label'])

格式如下:

[{“id”:“1”,“label”:“abc”,“icon”:{“uri”:“imagepath”}]

所以现在我要存储它,并尝试将标签值保存在变量中

    for(let i = 0; i < 4; i++){
 const json=this.state.typeofCountry[i];
  const originallable33 = json['label']; 
  marqueeItems.push(
    <View>
       <Text >
       {originallable33}
      </Text>
     </View>
  );
}
for(设i=0;i<4;i++){
const json=this.state.typeofCountry[i];
const originallable33=json['label'];
MarqueeeItems.push(
{originallable33}
);
}

如果有人向我解释如何解析数组和对象并从中获取任何特定值,那将非常有帮助。

这是因为
this.state.typeofCountry[i]
返回未定义的值 (很可能是由于
i
比数组本身大)

通过添加简单的空检查,安全地提取没有未定义错误的项-

for(设i=0;i<4;i++){
const json=this.state.typeofCountry[i];//json可能未定义
if(json?.label){
//注意?运算符(这称为可选链接)
//如果为true,则显示“标签”
const originallable33=json['label'];
MarqueeeItems.push(
{originallable33}
);
}

这是由于
this.state.typeofCountry[i]
返回未定义 (很可能是由于
i
比数组本身大)

通过添加简单的空检查,安全地提取没有未定义错误的项-

for(设i=0;i<4;i++){
const json=this.state.typeofCountry[i];//json可能未定义
if(json?.label){
//注意?运算符(这称为可选链接)
//如果为true,则显示“标签”
const originallable33=json['label'];
MarqueeeItems.push(
{originallable33}
);
}

看起来您正在尝试获取一个数据数组并使用它创建一个使用此数据的新组件数组。使用
for
循环可能非常庞大且难以读取,因此我认为您应该使用
map
功能

这就是我解决问题的方法:

// this.state.typeofCountry is an array so we 
// can use the map function to iterate over it.
// The callback function passes each entry of the array as a parameter, 
// you can name this parameter anything you want!

this.state.typeofCountry.map((country) =>  (
    <View>
       <Text >
       {country.label}
      </Text>
     </View>
))
//this.state.typeofCountry是一个数组,因此
//可以使用map函数对其进行迭代。
//回调函数将数组的每个条目作为参数传递,
//您可以随意命名此参数!
this.state.typeofCountry.map((国家)=>(
{country.label}
))
要了解更多有关map和其他真正有用的数组函数的信息,我建议您转到MDN,看看还可以使用哪些函数


希望这会有帮助,快乐编码!

看起来您正在尝试获取一个数据数组并使用它创建一个使用此数据的新组件数组。使用
for
循环可能会非常庞大且难以阅读,因此我认为您应该使用
map
功能

这就是我解决问题的方法:

// this.state.typeofCountry is an array so we 
// can use the map function to iterate over it.
// The callback function passes each entry of the array as a parameter, 
// you can name this parameter anything you want!

this.state.typeofCountry.map((country) =>  (
    <View>
       <Text >
       {country.label}
      </Text>
     </View>
))
//this.state.typeofCountry是一个数组,因此
//可以使用map函数对其进行迭代。
//回调函数将数组的每个条目作为参数传递,
//您可以随意命名此参数!
this.state.typeofCountry.map((国家)=>(
{country.label}
))
要了解更多有关map和其他真正有用的数组函数的信息,我建议您转到MDN,看看还可以使用哪些函数


希望对您有所帮助,祝您编码愉快!

如果这些回答中有任何一个回答了您的问题,请接受其中一个,寻求帮助而不接受答复是不礼貌的answer@PeterPocket,在苦苦挣扎之后,我遇到了真正的问题..这是我的api响应经常延迟,渲染设置得更早..因此数据没有显示…因为我是新手我需要努力找出真正的问题谢谢你的帮助没问题,祝你好运!如果这些回答中的任何一个回答回答了你的问题,请接受其中一个,寻求帮助而不接受答复是不礼貌的answer@PeterPocket,在苦苦挣扎之后,我遇到了真正的问题..是我的api响应习惯了迟到和重新启动nder设置得更早…因此数据没有显示…因为我是新手,我需要努力找出实际问题…感谢您的帮助没有问题,祝您好运!