JS递归遍历路由文件,修改hidden属性

JS递归遍历路由文件,修改hidden属性

根据 titles = [ { title: ‘立即巡店’ },{ title: ‘普通巡检配置’ },{ title: ‘图片巡店任务’ },];
上面是菜单权限,当路由文件匹配到对应的菜单名字,则说明有权限查看,hidden设置为false显示

function updateRoutePermissions(routes, titles) {
    // 创建一个 Set 用于快速查找 title
    const titleSet = new Set(titles.map(item => item.title));

    function hasPermission(title) {
        return titleSet.has(title);
    }

    function updateRoutes(routes) {
        routes.forEach(route => {
            if (route.children && route.children.length > 0) {
                // 如果有 children 则递归处理
                updateRoutes(route.children);
                // 检查 children 中是否有权限
                const childrenHasPermission = route.children.some(child => !child.hidden);
                route.hidden = !childrenHasPermission;
            } else {
                // 没有 children 则检查自身权限
                route.hidden = !hasPermission(route.title);
            }
        });
    }

    // 开始递归更新
    updateRoutes(routes);
}

// 示例数据
const titles = [
    
    { title: '立即巡店' },
	{ title: '普通巡检配置' },
	{ title: '图片巡店任务' },
];

const asyncAddRoutes = [
    {
        title: "巡店分析",
        children: [
            { title: "巡店统计" }
        ]
    },
    { title: "立即巡店" },
    {
        title: "巡店任务",
        children: [
            { title: "图片巡检任务" ,children:[{ title: "图片巡检配置" },{ title: "图片巡店任务" }] },
            { title: "普通巡检任务",children:[{ title: "普通巡检配置" },{ title: "普通巡店任务" }]  }
        ]
    },
	 
];

// 更新路由权限
updateRoutePermissions(asyncAddRoutes, titles);

// 打印结果查看
console.log(JSON.stringify(asyncAddRoutes, null, 2));

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值