0

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?

2
  • is obvious as you are overwriting it, whats the content of jobs? Commented May 16, 2017 at 15:14
  • its my json responce. ' [{"Vehicle_Details":"Civic","Transmission":"Manual"},{"Vehicle_Details":"fit","Transmission":"Manual"}]' Commented May 16, 2017 at 15:18

1 Answer 1

1

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']
    })
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.