简体   繁体   English

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

[英]How to go through JSON Array and add Data in to array - react-native

Im tring to add json object to array.so i can add array to my Flat list compoentnt.but i cant understand how to add data array. 我试图将json对象添加到array.so,我可以将array添加到我的平面列表compoentnt。但是我不明白如何添加数据数组。

this is my code 这是我的代码

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])
                    })
                }

but this added last object only.how can i solve this? 但这仅添加了最后一个对象。我该如何解决?

As you are parsing JSON in constructor itself. 当您在构造函数本身中解析JSON时。 there is no need to call setState , you can directly assign state. 无需调用setState ,可以直接分配状态。

Try this: 尝试这个:

constructor(props) {
  super(props);    

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM