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 ?
CodePudding user response:
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; }
CodePudding user response:
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