0

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?

3
  • Can you try this.state.fullForm.MyFormName Commented Sep 9, 2019 at 5:51
  • Object.keys(this.state.fullForm). MyFormName.map Commented Sep 9, 2019 at 5:51
  • MyFormName can be changed according to the response type. So i cannot directly use the name Commented Sep 9, 2019 at 5:57

1 Answer 1

1
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

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.