在React JS中循环或映射

时间:2018-08-20 17:04:44

标签: javascript reactjs ecmascript-6 jsx

我需要将此东西放入循环或map中。请帮忙。我不想提供数组的索引号。

<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>

2 个答案:

答案 0 :(得分:2)

使用render()函数尝试以下map方法:

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>
        )
    )
}

答案 1 :(得分:0)

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>
)}