嵌套数据处理
后端给的数据:
"sidebarRouters": [
{
"key": "0",
"icon": "el-icon-s-home",
"title": "首页",
"url": "/index"
},
{
"key": "1",
"icon": "el-icon-s-tools",
"title": "系统管理",
"url": "/systemManage",
"children": [
{
"key": "0",
"icon": "el-icon-menu",
"title": "菜单管理",
"url": "/menuManage"
},
{
"key": "1",
"icon": "el-icon-s-custom",
"title": "部门管理",
"url": "/partManage"
},
{
"key": "2",
"icon": "el-icon-suitcase",
"title": "岗位管理",
"url": "/jobManage"
},
{
"key": "3",
"icon": "el-icon-notebook-2",
"title": "日志管理",
"url": "/dayManage",
"children": [
{
"key": "0",
"icon": "el-icon-thumb",
"title": "操作日志",
"url": "/operationManage"
},
{
"key": "1",
"icon": "el-icon-s-custom",
"title": "登陆日志",
"url": "/loginManage"
}
]
}
]
},
{
"key": "2",
"icon": "el-icon-user",
"title": "用户管理",
"url": "/useManage"
},
{
"key": "3",
"icon": "el-icon-help",
"title": "其他",
"url": "/other",
"children": [
{
"key": "0",
"icon": "el-icon-date",
"title": "其他1",
"url": "/other1"
},
{
"key": "1",
"icon": "el-icon-paperclip",
"title": "其他2",
"url": "/other2"
}
]
}
]
目标:把所有的url取出作为单独数组
function filterPermissions(navMenuData) {
let children = []
navMenuData.forEach(el => {
if (el.children && el.children.length) {
return el.children.forEach(c => {
if (c.children && c.children.length) {
return (children = children.concat(filterPermissions(c.children)))
}
children.push(c.url)
})
} else {
children.push(el.url)
}
})
return children
}
const permissionList = filterPermissions(sidebarRouters)
console.log(permissionList)
结果: ['/index', '/menuManage', '/partManage', '/jobManage', '/operationManage', '/loginManage', '/useManage', '/other1', '/other2']

浙公网安备 33010602011771号