我试图弄清楚如何对字段数组使用react-hook-forms。我有工作的表格,但现在正在尝试弄清楚如何呈现数据。
我可以将json数据包记录为:
"ethics": {
"0": {
"explain": "df",
"managementPlan": "sdf"
},
"1": {
"explain": "sdf",
"managementPlan": ""
},
"value": "informedconsent",
"label": "Informed consent"
}
然后,在我的显示中,我尝试如下遍历每个数组:
{state.data.ethics.each.map(ethics => <Tag color="magenta">{ethics.label}</Tag>)}
这不起作用-错误消息说:
TypeError:无法读取未定义的属性'map'
我需要做什么来显示输出?
存在该错误,因为对象内没有数组each
。要遍历对象,请使用javascript的for ... in
。
for (let key in ethics) {
// skip loop if the property is from prototype
if (!ethics.hasOwnProperty(key)) continue;
let obj = ethics[key]; //label can be here//
for (var prop in obj) {
// skip loop if the property is from prototype
if (!obj.hasOwnProperty(prop)) continue;
// your code //
}
}
您可以添加逻辑使其正常工作。