'How to create group by module and submodule .TypeScript
i need help in this situation with Group By: I have this list
role = {
_id: '1',
name: 'Dorant',
description: 'For people',
organizationid: '-client',
permissions: [
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Buildings',
subModule: 'Areas',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Buildings',
subModule: 'Areas',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetAreas',
displayName: 'Get All Areas',
module: 'Buildings',
subModule: 'OSS',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Buildings',
subModule: 'FTT',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Users',
subModule: 'FDD',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Users',
subModule: 'SCH',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Sites',
subModule: 'Devices',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Sites',
subModule: 'Data Sources',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetBuildings',
displayName: 'Get All Buildings',
module: 'Sites',
subModule: 'Systems',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
},
{
name: 'GetDiqka',
displayName: 'Get All Buildings',
module: 'Sites',
subModule: 'Systems',
permissionType: 'READ',
constraints: [
{
constraint: 'AssignedInBuilding',
isSelected: false
}
],
dependecies: [],
isSelected: false
}
]
};
Here you see some permissions with theyrs module and submodules. One module have some submodules and one submodule and some permissions. I need to group them in some way to make a new array of the model below. I want to group them somehow to make a array like this:
moduleName: '',
subModules: [{
subMOduleName: '',
permissions: [{
}]
}];
}`
With the first list
Solution 1:[1]
Disclaimer : you should show what you have tried and answerers would explain what is wrong. In the absence of code, I will not provide the complete answer because StackOverflow is not intended for that. Instead, I give you the beginning and explain how to continue.
If I understand the model, you have a list of permissions. Each contain an object with a module and a submodule and you want to reorganize this by module/submodule.
Start by creating an empty array of modules then iterate on all permissions with a forEach :
const modules = [];
role.permissions.forEach((permission) => {
[...]
// here, you will populate the array of modules
}
Now, the module's name is permisssion.module. You want to check if the module is already present in the variable modules and add it if it does not exist.
const modules = [];
role.permissions.forEach((permission) => {
const moduleName = permission.module;
// Try to get the module. If it does not exist, create it.
let module = modules.find(m => m.moduleName === moduleName);
if(!module) {
module = {
moduleName: moduleName,
subModules: []
}
modules.push(module);
}
/*
* From this point the variable "module" contains the module and
* it exists in the array of modules. You will now want to populate
* its submodules
*
* Same logic goes for submodules
* Parse module.subModules in order to see if there is a submodule
* with subModuleName === permission.subModule
* Create it if it does not exist and push it to the array of subModules
*/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arnaud Denoyelle |
