简体   繁体   中英

Map JSON object - ReactJS

I am a getting a json object as a response and console log shows the response like this

MyFormName: Array(5)
0: {question: "Q1", type: "star"}
1: {question: "Q1", type: "star"}
2: {question: "Q1", type: "star"}
3: {question: "Q1", type: "star"}
4: {question: "Q1", type: "star"}

How can i map through this object so i can show data in each object?

I have tried something like this. I have my json object in a state fullForm and tried the following approach

return Object.keys(this.state.fullForm).map((item, key) => {

      return (
        <div className="overflow_hidden" key={key}>
          <div>{key + 1}</div>
          <div>{item.question}</div>

        </div>
      )

      });

But here the value i get for item is MyFormName . I want to map the objects in MyFormName . How can i do this?

this.state.fullForm[Object.keys(this.state.fullForm)[0]].map(item, key) => {
  //your logic here
})

It will get you the first item of fullForm which is in this example MyFormName's value as in an array and then you do .map

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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