Pratik Kumar
Pratik Kumar

Reputation: 69

Loop or map in React JS

I need this thing into a loop or map.please help. I don't want to provide the index number for arrays.

<div id="question">
{this.props.questions[0].id} :- {this.props.questions[0].text}
<h3>Correct Answer:- {this.props.questions[0].correct}</h3>
<h3>Your Answer:- {this.props.useranswer[0]}</h3>
</div>

Upvotes: 0

Views: 50

Answers (2)

Prijesh Meppayil
Prijesh Meppayil

Reputation: 540

Try the below render() method with map function:

render() {
    const { questions, useranswer } = this.props;
    return(
        questions.map((question, i)=>
            <div id="question">
            {question.id} :- {question.text}
            <h3>Correct Answer:- {question.correct}</h3>
            <h3>Your Answer:- {useranswer[i]}</h3>
            </div>
        )
    )
}

Upvotes: 2

Chris Cui
Chris Cui

Reputation: 117

You use the map function in the render() function. Like
{ this.props.questions.map((question)=>{
<div id="question">
{question.id} :- {question.text}
<h3>Correct Answer:- {question.correct}</h3>
<h3>Your Answer:- {get answer from question id}</h3>
</div>
)}

Upvotes: 0

Related Questions