Each user's permissions will vary based on their roles. My goal is to create an object structure like this:
let permissions = {
'state': {
'tool': ['subTool1', 'subTool2']
}
}
Here is an example:
roles = ['NY_email_submit', 'NY_email_approve', 'NY_build_submit', 'NY_build_view', 'DC_email_submit']
let permissions = {
'NY': {
'email': ['submit', 'approve'],
'build': ['submit', 'view']
},
'DC': {
'email': ['submit']
}
};
I am iterating through a list named roles, which contains strings in the format of state_tool_subTool
.
I want to avoid duplicates. For instance, if the next user role processed is NY_build_approve, I simply want to add approve to the list at ['build'].
At present, the following code snippet is not functioning correctly.
roles.forEach(role => {
role = role.split('_');
let state = role[0];
let tool = role[1];
let subTool = role[2];
if ([state] in permissions) {
permissions[state] = { [`${tool}`]: [subTool] };
} else {
//permissions[state][`${tool}`].push(subTool);
}
});