将权限阵列转换为JSON

发布于 2025-02-11 19:32:20 字数 2866 浏览 1 评论 0原文

我正在尝试在我的Nestjs项目中实现基于角色的访问控制,并且我有一个权限阵列如下

let permissions = [
            "newsfeeds-alerts-view",
            "settings-group_details-view",
            "settings-group_details-edit",
            "settings-privileges-view",
            "settings-privileges-edit",
            "settings-my_groups-create",
            "settings-my_groups-view",
            "settings-my_groups-edit",
            "settings-my_groups-delete",
            "settings-users-view",
            "settings-users-edit",
            "settings-users-delete",
            "notifications-email-create",
            "notifications-jira-create",
            "notifications-jira-view",
            "notifications-non_itr_ticket-create",
            "notifications-non_itr_ticket-update",
            "workspace_dashboard-worksapce-create",
            "workspace_dashboard-worksapce-view",
            "dashboard-geographic-maps-view",
            "dashboard-geographic-report-itr-view",
            "configurations-create_alerts-create",
            "configurations-notifications-jira-create"
];

我想从上面的数组创建一个JSON字符串,如下

{
  "newsfeeds": {
    "alerts": [
      {
        "name": "view"
      }
    ]
  },
  "settings": {
    "group_details": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      }
    ],
    "privileges": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      }
    ],
    "my_groups": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      },
      {
        "name": "delete"
      }
    ],
    "users": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      },
      {
        "name": "delete"
      }
    ]
  },
  "notifications": {
    "email": [
      {
        "name": "create"
      }
    ],
    "jira": [
      {
        "name": "create"
      },
      {
        "name": "view"
      }
    ],
    "non_itr_ticket": [
      {
        "name": "create"
      },
      {
        "name": "update"
      }
    ]
  },
  "workspace_dashboard": {
    "worksapce": [
      {
        "name": "create"
      },
      {
        "name": "view"
      }
    ]
  },
  "dashboard": {
    "geographic": {
      "maps": [
        {
          "name": "view"
        }
      ],
      "report": {
        "itr": [
          {
            "name": "view"
          }
        ]
      }
    },
    "configurations": {
      "create_alerts": [
        {
          "name": "create"
        }
      ],
      "notifications": {
        "jira": [
          {
            "name": "create"
          }
        ]
      }
    }
  }
}

权限阵列是动态的。我需要在一个对象下将所有常见的权限分组。

如何实现这一目标,任何简单的方法可用?

I'm trying to implement Role Based Access control in my nestjs project and I have a permission array as follows

let permissions = [
            "newsfeeds-alerts-view",
            "settings-group_details-view",
            "settings-group_details-edit",
            "settings-privileges-view",
            "settings-privileges-edit",
            "settings-my_groups-create",
            "settings-my_groups-view",
            "settings-my_groups-edit",
            "settings-my_groups-delete",
            "settings-users-view",
            "settings-users-edit",
            "settings-users-delete",
            "notifications-email-create",
            "notifications-jira-create",
            "notifications-jira-view",
            "notifications-non_itr_ticket-create",
            "notifications-non_itr_ticket-update",
            "workspace_dashboard-worksapce-create",
            "workspace_dashboard-worksapce-view",
            "dashboard-geographic-maps-view",
            "dashboard-geographic-report-itr-view",
            "configurations-create_alerts-create",
            "configurations-notifications-jira-create"
];

I want to create a JSON string from the above array as follows

{
  "newsfeeds": {
    "alerts": [
      {
        "name": "view"
      }
    ]
  },
  "settings": {
    "group_details": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      }
    ],
    "privileges": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      }
    ],
    "my_groups": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      },
      {
        "name": "delete"
      }
    ],
    "users": [
      {
        "name": "view"
      },
      {
        "name": "edit"
      },
      {
        "name": "delete"
      }
    ]
  },
  "notifications": {
    "email": [
      {
        "name": "create"
      }
    ],
    "jira": [
      {
        "name": "create"
      },
      {
        "name": "view"
      }
    ],
    "non_itr_ticket": [
      {
        "name": "create"
      },
      {
        "name": "update"
      }
    ]
  },
  "workspace_dashboard": {
    "worksapce": [
      {
        "name": "create"
      },
      {
        "name": "view"
      }
    ]
  },
  "dashboard": {
    "geographic": {
      "maps": [
        {
          "name": "view"
        }
      ],
      "report": {
        "itr": [
          {
            "name": "view"
          }
        ]
      }
    },
    "configurations": {
      "create_alerts": [
        {
          "name": "create"
        }
      ],
      "notifications": {
        "jira": [
          {
            "name": "create"
          }
        ]
      }
    }
  }
}

The permission array is dynamic. I need to group all common permission under one object.

How to achieve this, any easy methods available ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2025-02-18 19:32:20

使用array.Reduce string.string.split实现您所需的结果

let permissions = ["newsfeeds-alerts-view","settings-group_details-view","settings-group_details-edit","settings-privileges-view","settings-privileges-edit","settings-my_groups-create","settings-my_groups-view","settings-my_groups-edit","settings-my_groups-delete","settings-users-view","settings-users-edit","settings-users-delete","notifications-email-create","notifications-jira-create","notifications-jira-view","notifications-non_itr_ticket-create","notifications-non_itr_ticket-update","workspace_dashboard-worksapce-create","workspace_dashboard-worksapce-view","dashboard-geographic-maps-view","dashboard-geographic-report-itr-view","configurations-create_alerts-create","configurations-notifications-jira-create",];


const output = permissions.reduce((r, s) => {
    const path = s.split("-");
    if (path.length > 1) {
        const name = path.pop();
        const last = path.pop();
        let destination = r;
        for (let key of path) {
            destination[key] = destination[key] || {};
            destination = destination[key];
        }
        destination[last] = destination[last] || [];
        destination[last].push({ name });
    }
    return r;
}, {});
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Using array.reduce string.split to achieve your desired result

let permissions = ["newsfeeds-alerts-view","settings-group_details-view","settings-group_details-edit","settings-privileges-view","settings-privileges-edit","settings-my_groups-create","settings-my_groups-view","settings-my_groups-edit","settings-my_groups-delete","settings-users-view","settings-users-edit","settings-users-delete","notifications-email-create","notifications-jira-create","notifications-jira-view","notifications-non_itr_ticket-create","notifications-non_itr_ticket-update","workspace_dashboard-worksapce-create","workspace_dashboard-worksapce-view","dashboard-geographic-maps-view","dashboard-geographic-report-itr-view","configurations-create_alerts-create","configurations-notifications-jira-create",];


const output = permissions.reduce((r, s) => {
    const path = s.split("-");
    if (path.length > 1) {
        const name = path.pop();
        const last = path.pop();
        let destination = r;
        for (let key of path) {
            destination[key] = destination[key] || {};
            destination = destination[key];
        }
        destination[last] = destination[last] || [];
        destination[last].push({ name });
    }
    return r;
}, {});
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

假情假意假温柔 2025-02-18 19:32:20
let obj: any = {}

permissions.forEach(permission => {
    const [category, subcategory, name] = permission.split('-')
    if (!obj[category]) {
        obj[category] = {}
    }
    if (!obj[category][subcategory]) {
        obj[category][subcategory] = []
    }
    obj[category][subcategory].push({ name })
})

let json = JSON.stringify(obj) // there you go
let obj: any = {}

permissions.forEach(permission => {
    const [category, subcategory, name] = permission.split('-')
    if (!obj[category]) {
        obj[category] = {}
    }
    if (!obj[category][subcategory]) {
        obj[category][subcategory] = []
    }
    obj[category][subcategory].push({ name })
})

let json = JSON.stringify(obj) // there you go
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文