在native native中使用嵌套对象访问多维数组

时间:2018-01-12 10:08:36

标签: react-native

我想在我的应用中访问多维数组。我的数组就像: -

[{"productid":"31369","productname":"Abilene Swivelling ","productcode":"MTN-CLR.OW","selectoptions":[{"OptionName":"Bracket Option","OptionValue":[{"id":"1668","text":"Without Sound Bar Bracket"},{"id":"1669","text":"With Sound Bar Bracket"}]}]}]

我想访问selectoptions。我已获取productid但无法获取选择选项。 我试过这个: -

{this.props.data.map((dataImage,Index)=>
                <TouchableOpacity>
                        <View key={dataImage['productid']} style={productStyle.homeimg1}>                           
                            <Text style={productStyle.title}> {dataImage['productname']}</Text>
                            <Text style={productStyle.pricesecleft}>RRP {dataImage['productrrp']}</Text>
</View>
{dataImage['selectoptions'].map((selecval) =>
<View>
                            <Text>{selecval['OptionName']}</Text></View>

                            )}
  </View>
                </TouchableOpacity>
                )}

它不起作用。返回错误

1 个答案:

答案 0 :(得分:2)

您必须使用return语句才能在地图功能

中返回数据
   {this.props.data.map((dataImage,Index)=>
        return (
          <TouchableOpacity key={Index}>
                <View key={dataImage['productid']} style={productStyle.homeimg1}>                           
                    <Text style={productStyle.title}> {dataImage['productname']}</Text>
                    <Text style={productStyle.pricesecleft}>RRP {dataImage['productrrp']}</Text>
                </View>
                {
                   console.log(dataImage['selectoptions'])
                   dataImage['selectoptions'].map((selecval, Index2) =>
                    return (
                      <View key={Index2}>
                        <Text>{selecval['OptionName']}</Text>
                        {selecval['OptionValue'].map((optionval, Index3) =>
                          return (
                              <View key={Index3}>
                                <Text>{optionval['text']}</Text>
                              </View>
                          )
                        }
                      </View>
                    )
                )}
      </View>
      </TouchableOpacity>
        )                    
     )}

希望这会有所帮助。

相关问题