多层级数组对象的筛选,要求保留层级(权限匹配)

皮卡丘UP 2020-04-03 12:10:51
要求根据下面powerKey数组中的值去匹配 MenuList数组中的moduleMark,返回匹配到的层级结构
let powerKey=["generalizationSee","generalization","student_attendanceStatisticsSee","attendance","student_attendance","student_attendanceStatistics","student_leave_requestSee","student_leave_request","student_attendanceSetEdit","student_attendanceSet","student_attendanceSetSee","attendanceStatisticsSee","staff_attendance","attendanceStatistics","approval_processSee","approval_process","approval_processEdit","attendanceSetEdit","attendanceSet","attendanceSetSee","time_cardEdit","hardware","time_card","time_cardSee","machineSee","machine","machineEdit","classEdit","educational_administration","routine_educational_administration"]


let MenuList=[
{
name:'概况',
path:'/situation',
moduleMark:'generalization'
},{
name:'考勤管理',
moduleMark:'attendance',
children:[
{
name:'学员出勤',
moduleMark:'student_attendance',
children: [
{
name:'出勤统计',
path:'/studentStatistics',
moduleMark:'student_attendanceStatistics'
}, {
name:'请假申请',
path:'/leaveManage',
moduleMark:'student_leave_request'
},{
name:'考勤设置',
path:'/studentSetting',
moduleMark:'student_attendanceSet'
}
]
},{
name:'教职工考勤',
moduleMark:'staff_attendance',
children:[
{
name:'考勤统计',
path:'/staffStatistics',
moduleMark:'attendanceStatistics'
}, {
name:'流程审批',
path:'/processApproval',
moduleMark:'approval_process'
},{
name:'考勤设置',
path:'/staffSetting',
moduleMark:'attendanceSet'
}
]
},{
name:'硬件管理',
moduleMark:'hardware',
children:[
{
name:'考勤卡管理',
path:'/cardManage',
moduleMark:'time_card'
}, {
name:'设备管理',
path:'/deviceManage',
moduleMark:'machine'
}
]
}
]
},{
name:'教务管理',
moduleMark:'educational_administration',
children:[
{
name:'常规教务',
moduleMark:'routine_educational_administration',
children:[
{
name:'班级',
path:'/classManage',
moduleMark:'class'
},{
name:'课程',
path:'/course',
moduleMark:'course'
}
]
},{
name:'日常园务',
moduleMark:'',
children:[
{
name:'图书管理',
path:'/booksManage',
moduleMark:''
},{
name:'通知发布',
path:'/noticeRelease',
moduleMark:''
}
]
}
]
},{
name:'课程销售',
moduleMark:'course_sales',
children:[
{
name:'销售管理',
moduleMark:'sales_management',
children:[
{
name:'课程销售',
path:'/courseSales',
moduleMark:'course_sales2'
},{
name:'订单查询',
path:'/orderSearch',
moduleMark:'order_administration'
}
]
}
]
},{
name:'学员管理',
moduleMark:'student_management',
children:[
{
name:'学员管理',
moduleMark:'student_administration',
children:[
{
name:'学员信息',
path:'/studentInfo',
moduleMark:'student_information'
},{
name:'历史变更',
path:'/historicalChange',
moduleMark:'historical_change'
},{
name:'历史学员',
path:'/historyStudent',
moduleMark:'history_student'
}
]
}
]
},{
name:'更多',
moduleMark:'more',
children:[
{
name:'组织管理',
moduleMark:'organization_management',
children:[
{
name:'校区管理',
path:'/campusManage',
moduleMark:'campus'
}, {
name:'账号管理',
path:'/accountManage',
moduleMark:'workers'
}
]
},{
name:'财务管理',
moduleMark:'financial_management',
children:[
{
name:'收款账户',
path:'/collectionAccount',
moduleMark:'receiving_account'
}
]
},{
name:'系统日志',
moduleMark:'receiving_account',
children:[
{
name:'登录日志',
path:'/loginLog',
moduleMark:'login_log'
},{
name:'操作记录',
path:'/operationLog',
moduleMark:'operation_log'
}
]
}
]
}
];
...全文
808 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
RopeHuo 2020-07-27
  • 打赏
  • 举报
回复

if ((!node.children || node.children.length === 0) && hasPower(node) === false) {
     parent.splice(index);
}
是不是应该改成

if ((!node.children || node.children.length === 0) && hasPower(node) === false) {
     parent.splice(index,1);
}
如果不是的话为啥要把数组中的数据从index下标开始全部删除完呢?
皮卡丘UP 2020-04-03
  • 打赏
  • 举报
回复
引用 2 楼 Hello World, 的回复:
过滤
    function filteNode(node, parent, index) {
        if (node instanceof Array) {
            for (var i = node.length - 1; i >= 0; i--) {
                filteNode(node[i], node, i);
            }
        } else {
            if (node.children) {
                for (var i = node.children.length - 1; i >= 0; i--)
                    filteNode(node.children[i], node.children, i);
            }
            if ((!node.children || node.children.length === 0) && hasPower(node) === false) {
                parent.splice(index);
            }
        }
    }
    function hasPower(node) {
        var key = node.moduleMark;
        for (var i = 0, n = powerKey.length; i < n; i++) {
            if (powerKey[i] === key)
                return true;
        }
        return false;
    }
    filteNode(MenuList);
    console.log(JSON.stringify(MenuList));
谢谢!可以的!
皮卡丘UP 2020-04-03
  • 打赏
  • 举报
回复
引用 1 楼 jio可 的回复:
循环powerKey +递归MenuList匹配到就在menuList对象加上show=true的属性,然后在生成菜单的时候多加个条件显示show==true的菜单
感谢提供思路
Hello World, 2020-04-03
  • 打赏
  • 举报
回复
过滤
    function filteNode(node, parent, index) {
if (node instanceof Array) {
for (var i = node.length - 1; i >= 0; i--) {
filteNode(node[i], node, i);
}
} else {
if (node.children) {
for (var i = node.children.length - 1; i >= 0; i--)
filteNode(node.children[i], node.children, i);
}
if ((!node.children || node.children.length === 0) && hasPower(node) === false) {
parent.splice(index);
}
}
}
function hasPower(node) {
var key = node.moduleMark;
for (var i = 0, n = powerKey.length; i < n; i++) {
if (powerKey[i] === key)
return true;
}
return false;
}
filteNode(MenuList);
console.log(JSON.stringify(MenuList));
jio可 2020-04-03
  • 打赏
  • 举报
回复
循环powerKey +递归MenuList匹配到就在menuList对象加上show=true的属性,然后在生成菜单的时候多加个条件显示show==true的菜单

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧