【问题标题】:Loop or map in React JSReact JS 中的循环或映射
【发布时间】:2018-08-20 17:04:43
【问题描述】:
我需要这个东西进入循环或地图。请帮助。我不想提供数组的索引号。
<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>
【问题讨论】:
标签:
javascript
reactjs
ecmascript-6
jsx
【解决方案1】:
用map函数试试下面的render()方法:
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>
)
)
}
【解决方案2】:
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>
)}