为什么只判断 children 数据中的第一个
来源:8-7 菜单列表递归实现(下)

慕粉3946981
2022-02-28
// 递归拼接树形列表
function getTreeMenu(rootList, id, list) {
for (let i = 0; i < rootList.length; i++) {
let item = rootList[i]
if (String(item.parentId.slice().pop()) == String(id)) {
list.push(item._doc)
}
}
list.forEach(item => {
item.children = []
getTreeMenu(rootList, item._id, item.children)
if (item.children.length) {
delete item.children
} else if (item.children.length > 0 && item.children[0].menuType == 2) {
// 快速区分按钮和菜单 用于后期做菜单按钮权限控制
item.action = item.children
delete item.children
}
})
return list
}
- 请问这块为什么判断 children 数组中的第一个就能判断后面的元素都是按钮?
写回答
1回答
-
因为按钮都是放在一起的,第一个是按钮,后面的肯定都是按钮,后端返回的时候,就把按钮放在一起丢进children里面的
022023-07-06
相似问题