从React native中的JSON响应中获取值的键

问题描述 投票:0回答:1

我有以下形式的JSON响应:我的问题是,我想在此响应“星期五”:“ 1”中显示每个值等于1的键,而忽略空值。我写了一个代码,仅显示与值1匹配的一个键,但是我想显示每个与值1匹配的键。请指导我。在此先感谢

{
"data": {
    "timetable": [
        {
            "id": 2,
            "tid": "91",
            "cid": "1",
            "monday": null,
            "tuesday": null,
            "wednesday": null,
            "thursday": null,
            "friday": "1",
            "saturday": null,
            "sunday": null,
            "ti1_2": null,
            "ti2_3": null,
            "ti3_4": null,
            "ti4_5": null,
            "ti5_6": null,
            "ti6_7": null,
            "ti7_8": null,
            "ti8_9": null,
            "ti9_10": null,
            "ti10_11": "10-11",
            "ti11_12": null,
            "ti12_13": null,
            "ti13_14": null,
            "ti14_15": null,
            "ti15_16": null,
            "ti16_17": null,
            "ti17_18": null,
            "ti18_19": null,
            "ti19_20": null,
            "ti20_21": null,
            "ti21_22": null,
            "ti22_23": null,
            "ti23_24": null,
            "created_at": "2019-02-15 10:27:57",
            "updated_at": "2019-02-15 10:27:57"
        }
    ]
},

}

我只想将时间表中的那几天设为'1'并显示在清单中。即“星期五”:“ 1”我尝试了一个代码这是方法

getKeyByValue = (object, value) => { 

  for (var key in object) {
    if(object[key] === value){
      return (<Text>{key}</Text>);
    }
}

}

平面项目的renderItem方法


    <View>
        <Text>{this.getKeyByValue(item , '1')}</Text>
    </View>
  );

and this is my flatlist

    <FlatList
                          data={this.state.data.timetable}
                           renderItem={this.renderItem}
                           horizontal={true}
                           keyExtractor={(item, index) => index}
                           contentContainerSty`le={{
                               flexGrow: 1,
                               }} />



javascript arrays json rest react-native
1个回答
0
投票

timetable是一个数组,因此您需要执行以下操作:

data={this.state.data.timetable[0]}

除了"1"之外,还有另一个值为friday的键,即cid,您可能要排除它。见下文:

let result = {
  "data": {
    "timetable": [{
      "id": 2,
      "tid": "91",
      "cid": "1",
      "monday": null,
      "tuesday": null,
      "wednesday": null,
      "thursday": null,
      "friday": "1",
      "saturday": null,
      "sunday": null,
      "ti1_2": null,
      "ti2_3": null,
      "ti3_4": null,
      "ti4_5": null,
      "ti5_6": null,
      "ti6_7": null,
      "ti7_8": null,
      "ti8_9": null,
      "ti9_10": null,
      "ti10_11": "10-11",
      "ti11_12": null,
      "ti12_13": null,
      "ti13_14": null,
      "ti14_15": null,
      "ti15_16": null,
      "ti16_17": null,
      "ti17_18": null,
      "ti18_19": null,
      "ti19_20": null,
      "ti20_21": null,
      "ti21_22": null,
      "ti22_23": null,
      "ti23_24": null,
      "created_at": "2019-02-15 10:27:57",
      "updated_at": "2019-02-15 10:27:57"
    }]
  }
};

getKeyByValue = (object, value) => {
  for (var key in object) {
    if (object[key] === value && key !== "cid") {
      return key;
    }
  }
}

console.log(getKeyByValue(result.data.timetable[0], "1"));
© www.soinside.com 2019 - 2024. All rights reserved.