题目描述
如:父级 包含 1个子级, 若子级ID 为6,那么子级与父级checked 均为 false
题目来源及自己的思路
function resetData(list, exceptId) {
if (Array.isArray(list) && list.length !== 0) {
list = list.map(item => {
if (item.id === exceptId) {
item.checked = false;
}
if (!item.isLeaf) {
resetData(item.children, exceptId);
}
return item;
});
return list;
} else {
return false;
}
}
相关代码
粘贴代码文本(请勿用截图)
const arr = [
{
"id": 4,
"name": "标题1",
"children": [
{
"id": 3,
"name": "1-1",
"children": null
}
]
},
{
"id": 5,
"name": "标题2",
"children": [
{
"id": 4,
"name": "2-1",
"checked": true,
"children": null
},
{
"id": 5,
"name": "2-2",
"children": null
}
]
},
{
"id": 7,
"name": "标题3",
"children": [
{
"id": 6,
"name": "3-1",
"checked": true,
"children": null
}
]
}
];
你期待的结果是什么?实际看到的错误信息又是什么?
子级 checked 全为 false,父级 为 false ;按其相对父子关系。返回一个新的数组对象。
如:执行:console.log(resetData(arr, 6));
每太看懂你的需求:
这两个需求,感觉是冲突的。第二个情况包含了第一个。
你写的能够保证符合的子级为false不能保证全选时,父级变为false。所以上面需要一个回馈所有子级已经变更的机制。
可以做一个闭包,接收回馈。
结果如图:
