Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何遍历JSON数组并将数据添加到数组中-react native_Arrays_Json_Reactjs_React Native - Fatal编程技术网

Arrays 如何遍历JSON数组并将数据添加到数组中-react native

Arrays 如何遍历JSON数组并将数据添加到数组中-react native,arrays,json,reactjs,react-native,Arrays,Json,Reactjs,React Native,我尝试将json对象添加到数组中。因此我可以将数组添加到平面列表组件中。但我不知道如何添加数据数组 这是我的密码 constructor(props) { super(props); this.state = { Vehicle_Details :[], } var text = JSON.parse(jobs); for (var i = 0; i < text.length; i

我尝试将json对象添加到数组中。因此我可以将数组添加到平面列表组件中。但我不知道如何添加数据数组

这是我的密码

constructor(props) {
        super(props);
        this.state = {
            Vehicle_Details :[],

        }

var text = JSON.parse(jobs);
                for (var i = 0; i < text.length; i++) {
                    console.log(text[i]["Vehicle_Details"]);
                    this.setState({
                    Vehicle_Details:(text[i])
                    })
                }

但这只添加了最后一个对象。我如何解决这个问题

当您在构造函数中解析JSON时。不需要调用setState,您可以直接分配状态

试试这个:

constructor(props) {
  super(props);    

  var text = JSON.parse(jobs);
  this.state = {
    Vehicle_Details: text.map(function(item) {
      return item['Vehicle_Details']
    })
  }
}

很明显,当你覆盖它时,作业的内容是什么?是我的json响应。”[{车辆详细信息:思域,变速器:手动},{车辆详细信息:安装,变速器:手动}]