let permissions = [
{
id: 1,
name: "Inventory",
permission: "inventory"
},
{
id: 2,
name: "Inventory read",
permission: "inventory:read"
},
{
id: 3,
name: "Purchase Orders",
permission: "purchase-orders"
},
{
id: 4,
name: "Purchase Orders create",
permission: "purchase-orders:create"
}
];
let roles = [
{
id: 1,
name: "Admin"
},
{
id: 2,
name: "Manager"
}
];
我需要按“权限”的第一部分对权限进行分组,该数组具有权限名称和角色的属性,每个角色都具有分组的权限
最初,我一直在将权限分组,方法是将组推送到groups [permission.split(':')],然后遍历权限并在循环内循环执行模板中的角色。
我现在需要首先构建数据。
我需要以下输出:
let list = [
{
permission: 'Inventory',
Admin: [
{
id: 1,
name: "Inventory",
permission: "inventory"
},
{
id: 2,
name: "Inventory read",
permission: "inventory:read"
},
],
Manager: [
{
id: 1,
name: "Inventory",
permission: "inventory"
},
{
id: 2,
name: "Inventory read",
permission: "inventory:read"
},
]
},
{
permission: 'Purchase Orders',
Admin: [
{
id: 3,
name: "Purchase Orders",
permission: "purchase-orders"
},
{
id: 4,
name: "Purchase Orders create",
permission: "purchase-orders:create"
}
],
Manager: [
{
id: 3,
name: "Purchase Orders",
permission: "purchase-orders"
},
{
id: 4,
name: "Purchase Orders create",
permission: "purchase-orders:create"
}
]
}
];