如何根据getcheckvalue数组匹配修改defaultArr的权限值?
匹配模块权限并更新默认权限数组
要实现将defaultArr中与getcheckvalue匹配的权限项设为true,可以按以下逻辑处理:
- 遍历
getcheckvalue中的每一条权限配置 - 在
defaultArr中定位到对应模块的对象 - 取出该模块下的所有操作项列表
- 找到匹配的操作项,将其布尔值改为
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会变为trueroles模块的update变为trueparentDevice模块的update变为truedeviceAlert模块的getAll变为truegeneral模块的device-types变为trueemployees模块的delete变为true
其余未匹配的权限项保持false不变。
内容的提问来源于stack exchange,提问作者Meet Majevadiya
相关产品推荐
相关产品推荐

