Javascript 反应js映射和过滤器
显示复选框列表,我必须根据阵列数据onload显示复选框状态 参考代码Javascript 反应js映射和过滤器,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,显示复选框列表,我必须根据阵列数据onload显示复选框状态 参考代码 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.AllGrou
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>
{this.state.AllGroups((项,索引)=>{
//这里需要条件
//if(item.id==userCatChecked.id){
//显示真复选框
//}否则{
//显示假复选框
//}
})}
{this.state.AllGroups.map((项,索引)=>
cat.id==item.id)}
onChange={}
/>
)}
{this.state.AllGroups.map((项,索引)=>
cat.id==item.id)}
onChange={}
/>
)}
这个怎么样
可以使用Array.some
函数检查对象数组是否包含项。
constallgroups=[{id:1,名称:'one'},{id:2,名称:'two'},{id:3,名称:'twree'},{id:4,名称:'four'}]
const userCatChecked=[{id:2,名称:'two'},{id:3,名称:'two'}]
const result=AllGroups.map((项目、索引)=>{
const checked=userCatChecked.some(checked=>checked.id==item.id);
返回{…项,选中};
});
log('result:',result)代码>这个怎么样
可以使用Array.some
函数检查对象数组是否包含项。
constallgroups=[{id:1,名称:'one'},{id:2,名称:'two'},{id:3,名称:'twree'},{id:4,名称:'four'}]
const userCatChecked=[{id:2,名称:'two'},{id:3,名称:'two'}]
const result=AllGroups.map((项目、索引)=>{
const checked=userCatChecked.some(checked=>checked.id==item.id);
返回{…项,选中};
});
log('result:',result)代码>处理复选框的另一种方法是通过
然后以你的形式
<form>
{this.state.AllGroups.map((item, index) =>
<input
type="checkbox"
checked={checkedItems.get(item.name)}
onChange={ }
/>
)}
</form>
{this.state.AllGroups.map((项,索引)=>
)}
这可能也会有帮助:)另一种处理复选框的方法是通过
然后以你的形式
<form>
{this.state.AllGroups.map((item, index) =>
<input
type="checkbox"
checked={checkedItems.get(item.name)}
onChange={ }
/>
)}
</form>
{this.state.AllGroups.map((项,索引)=>
)}
这可能也会有帮助:)所有组
都是数组,你不能称之为不理解,代码只是供参考所有组
都是数组,你不能称之为不理解,代码只是供参考工作正常,但是当我单击时复选框状态没有改变你应该提供一个onChange函数工作正常,但是,但是当我点击时复选框状态没有改变你应该提供一个onChange功能我没有试过这个,我用下面的答案测试了一下效果很好,谢谢你的帮助我没有试过这个,我用下面的答案测试了一下效果很好,谢谢你的帮助
<form>
{this.state.AllGroups.map((item, index) =>
<input
type="checkbox"
checked={checkedItems.get(item.name)}
onChange={ }
/>
)}
</form>