显示复选框列表,我必须根据数组数据加载显示复选框状态
引用代码
AllGroups = [{id:1, name:one},{id:2,name:two},{id:3, name:three},{id:4,name:four}]
userCatChecked = [{id:2,name:two},{id:3, name:three}]
复选框状态应根据 userCatChecked 数组匹配显示 true/false
<form>
{this.state.AllGroups((item, index) => {
// Here need condition
// if( item.id === userCatChecked.id ) {
// show true checkbox
//}else{
// show false checkbox
//}
<input type="checkbox" checked={true/false} onChange={ } />
})}
</form>
最佳答案
<form>
{this.state.AllGroups.map((item, index) =>
<input
type="checkbox"
checked={!!this.state.userCatChecked.find(cat => cat.id === item.id)}
onChange={ }
/>
)}
</form>
关于javascript - React js 映射和过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60752251/