1

Hello I have JSON structured like this, and I need to iterate over the items

[
  {
    "name": "About You",
    "questions": [
      {
        "questionText": "What is your surname?",
        "answers": [
          {
            "text": "Thomas"
          }
        ]
      },
      {
        "questionText": "Where do you work?",
        "answers": [
          {
            "text": "Finance"
          }
        ]
      },
    ]
  },
  {
    "name": "About Family",
    "questions": [
      {
        "questionText": "Childeren",
        "answers": [
          {
            "text": "Yes"
          }
        ]
      },
      {
        "questionText": "Married",
        "answers": [
          {
            "text": "No"
          }
        ]
      },
    ]
  },
  {
    "name": "Travel",
    "questions": [
      {
        "questionText": "Do you travel a lot?",
        "answers": [
          {
            "text": "Yes"
          }
        ]
      }
    ]
  }
]

I started with this code but I don't know how to show all the nested items. Should I use another map function? I would like to have format that will show table with questionTexts and answers text

{details.map((detail, i) => (
    <div key={i}>
      <div>{detail.name}</div>
      <div>{detail.questions ? detail.questions[0].questionText : ''}</div>
      <div>{detail.questions ? detail.questions[0].answers[0].text : ''}</div>
    </div>
  )
)}

thank you

2
  • 2
    Yea you will need a second .map Commented Jun 3, 2021 at 14:34
  • You'll want to use something other than index as key, detail.name would be better. Commented Jun 3, 2021 at 14:44

1 Answer 1

1

Should I use another map function?

Yes


  1. First, map through the details array
    details.map((detail, i) =>

  2. Map through the current detail.questions
    detail.questions.map((question) => (

  3. Optionally, apply the same logic for question.answers
    question.answers.map((question) => (

Small example were we show all the questions:

class Example extends React.Component { 
    render() {
        const  details = [{"name": "About You", "questions": [{"questionText": "What is your surname?", "answers": [{"text": "Thomas"} ] }, {"questionText": "Where do you work?", "answers": [{"text": "Finance"} ] }, ] }, {"name": "About Family", "questions": [{"questionText": "Childeren", "answers": [{"text": "Yes"} ] }, {"questionText": "Married", "answers": [{"text": "No"} ] }, ] }, {"name": "Travel", "questions": [{"questionText": "Do you travel a lot?", "answers": [{"text": "Yes"} ] } ] } ];
        
        return (
            details.map((detail, i) => (
                <div key={detail.name} class='question'>
                    <b>{detail.name}</b>
                    {
                        detail.questions.map((question) => (
                            <em key={question.questionText}>
                                Question: {question.questionText}
                            </em>
                        ))
                    }
                </div>
            ))
        );
    }
}

ReactDOM.render(<Example />, document.body);
.question {
  display: flex;
  flex-direction: column;
  margin-bottom: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Additional notes

  • Changed the key={} to the name of the question to we're sure it's unique
  • Added a key to the nested map(), using the question itself
    Thx to @pilchard for noticing
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helped! Please consider upvoting or accepting any answers that might have helped you. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself.

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.