我正在使用 DND5E API 并做出反应。我调用数据并将其保存到一个名为
primaryChoices
的变量中,该变量是一个从类部分下 api 中的 `proficiency_choices 开始的数组 (https://www.dnd5eapi.co/api/classes/monk)。
我用
.map
迭代它,然后使用 element.from.options
(也是一个数组),并使用嵌套 .map
迭代它并提取 options.item.index
的值。
这个逻辑适用于除僧侣之外的所有职业。它的设置与 api 中的所有其他类相同,甚至可以使用 this
在
.map
函数之外正确记录
console.log(primaryChoices[0].from.options[0].item.index)
同一个控制台日志也记录其他类,我什至可以毫无错误地显示它们的任何信息。
// after the data is called from the api it gets saved to character.class.primary
const primaryChoices = character?.class?.primary?.proficiency_choices
if(!isPrimaryEmpty && isSecondaryEmpty){
return (primaryChoices.map((element) => (
<div>
<strong>Choose {`${element.choose}`} for {`${primary.name}`}</strong>
{element.from.options.map((option) => {
return(
<p>
<input onChange={(e) => handleCheck(e, element, option)} type='checkbox' name={option.item.index} />
<label htmlFor={option.item.index}>{option.item.name}</label>
</p>
)
})}
</div>
)))
}else if(...){...}
element.choose
和 primary.name
都适用于任何课程
我检查了路径,它记录正确,没有抛出错误,我转到 api 页面并仔细检查了路径。
dfgghgfbfgbfgghggfggbgbgbdgbghngfdgnhdnfgngnfn