[英]Map JSON object - ReactJS
I am a getting a json object as a response and console log shows the response like this 我正在获取一个json对象作为响应,并且控制台日志显示了这样的响应
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 我的json对象处于
fullForm
状态,并尝试了以下方法
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
. 但是在这里,我为
item
获得的值是MyFormName
。 I want to map the objects in MyFormName
. 我想映射
MyFormName
的对象。 How can i do this? 我怎样才能做到这一点?
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
它会为您提供fullForm的第一项,在此示例中,它是数组中MyFormName的值,然后执行
.map
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.