递归遍历多级菜单 | 星雨笔录
使用递归处理任意层级的菜单树。
核心: 递归遍历树结构要处理“当前节点”和“子节点”,遇到叶节点自然停止向下遍历。
最小示例:
function printMenu(items) {
for (const item of items) {
console.log(item.title);
if (item.children?.length) printMenu(item.children);
}
}
学完能做: 使用递归处理任意层级的菜单树。