You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据getcheckvalue数组匹配修改defaultArr的权限值?

匹配模块权限并更新默认权限数组

要实现将defaultArr中与getcheckvalue匹配的权限项设为true,可以按以下逻辑处理:

  1. 遍历getcheckvalue中的每一条权限配置
  2. 在defaultArr中定位到对应模块的对象
  3. 取出该模块下的所有操作项列表
  4. 找到匹配的操作项,将其布尔值改为true

具体实现代码

// 遍历所有需要启用的权限配置
getcheckvalue.forEach(permission => {
    // 找到defaultArr中对应模块的对象
    const targetModule = defaultArr.find(moduleItem => {
        const moduleName = Object.keys(moduleItem)[0];
        return moduleName === permission.module;
    });

    if (targetModule) {
        // 获取模块下的操作项列表(跳过两层嵌套数组)
        const actionList = targetModule[permission.module][0];
        // 找到对应的操作项并设为true
        const targetAction = actionList.find(actionItem => {
            const actionName = Object.keys(actionItem)[0];
            return actionName === permission.action;
        });

        if (targetAction) {
            targetAction[permission.action] = true;
        }
    }
});

// 输出更新后的权限数组
console.log(defaultArr);

结果说明

执行这段代码后,defaultArr中:

  • users模块的add、change-password、delete、forgot-password会变为true
  • roles模块的update变为true
  • parentDevice模块的update变为true
  • deviceAlert模块的getAll变为true
  • general模块的device-types变为true
  • employees模块的delete变为true

其余未匹配的权限项保持false不变。

内容的提问来源于stack exchange,提问作者Meet Majevadiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:05:28