Javascript ES6:从JSON数组内部获取特定数组,并使用map对其进行迭代

时间:2018-07-03 07:40:19

标签: javascript json reactjs react-native ecmascript-6

我在https://api.myjson.com/bins/9jyq4有一个JSON

"response": [{
    "id": "1",
    "title": "Star Wars",
    "project": [
      "Star Wars Proj1",
      "Star Wars Proj2",
      "Star Wars Proj3",
      "Star Wars Proj4"
    ]
  },
  {
    "id": "2",
    "title": "Back to the Future",
    "project": [
      "Back to the Future Proj1",
      "Back to the Future Proj2",
      "Back to the Future Proj3",
      "Back to the Future Proj4"
    ]
  },
  ..
  ..

我有两个选择器。 “标题”和“项目” 我正在使用填充标题选择器。

<Picker
  mode="dialog"
  selectedValue={this.state.Movietitle}
  onValueChange={(itemValue, itemIndex) => 
  this.setState({ Movietitle: itemValue });
  }
  enabled={!this.state.inRide}
  >
  { 
    this.state.responseData.map((item) => (
    <Picker.Item label={item.title} value={item.id} key={item.id} />))
  }
</Picker>

因此,现在我要使用从第一个选择器中选择的选项来填充第二个选择器“项目”。只需要帮助进行地图查询。谢谢

2 个答案:

答案 0 :(得分:0)

使用过滤器方法获取ID匹配的对象的棕褐色数组,然后在其上使用map方法

let response = [{
    "id": "1",
    "title": "Star Wars",
    "project": [
      "Star Wars Proj1",
      "Star Wars Proj2",
      "Star Wars Proj3",
      "Star Wars Proj4"
    ]
  },
  {
    "id": "2",
    "title": "Back to the Future",
    "project": [
      "Back to the Future Proj1",
      "Back to the Future Proj2",
      "Back to the Future Proj3",
      "Back to the Future Proj4"
    ]
  }
]

function getObject(srchId) {
  let __obj = response.filter(function(item) {
    return item.id === srchId;


  })
  return __obj;
}

getObject('2').map(function(item) {

  //rest of the code
})

答案 1 :(得分:0)

您应该能够通过const string DeleteThisVersionDirectory访问当前选定的响应项目并简单地映射该响应项目的find属性中的每个项目来访问每个项目标题。

赞:

project