[英]how to map an Object.keys array inside an Object.keys in react.js?
I try:我尝试:
{Object.keys(questions).map(
(question, index) => (
<div className="row questions" key={index}>
<p>{questions[question].question_title}</p>
// HERE NOT WORK
{Object.keys(questions[question].answers).map(
(answer, index) => (
console.log(questions[question].answers[answer].id_answer)
)
)}
</div>
)
)}
this return "Cannot convert undefined or null to object"此返回“无法将未定义或 null 转换为对象”
this error indicates that you have question(s) where answers
property is undefined
or null
.此错误表明您有问题,其中answers
属性undefined
或null
。 This results in a call Object.keys(undefined)
which throws the error.这导致调用Object.keys(undefined)
抛出错误。
you need to add a safe guard for your code to not execute if that's the case:如果是这种情况,您需要为您的代码添加安全防护以使其不执行:
{ questions[question].answers && Object.keys(questions[question].answers).map(
(answer, index) => (
console.log(questions[question].answers[answer].id_answer)
)
)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.