Reputation: 739
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.
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?
Upvotes: 0
Views: 4371
Reputation: 2857
As you are parsing JSON in constructor itself. there is no need to call setState
, you can directly assign state.
Try this:
constructor(props) {
super(props);
var text = JSON.parse(jobs);
this.state = {
Vehicle_Details: text.map(function(item) {
return item['Vehicle_Details']
})
}
}
Upvotes: 1