I'm struggling to group my array of objects into another desired array based on a specific property. Despite checking out various tutorials, I couldn't achieve the expected output.
What I aim for is to group all elements according to their designated properties.
Below is my input:
permissions= [
{
code: 'U00',
permission_name: 'Read User',
groupBy: 'User',
icon: 'user',
},
{
code: 'U01',
permission_name: 'Create User',
groupBy: 'User',
icon: 'user',
},
{
code: 'B00',
permission_name: 'Read Batch',
groupBy: 'Batch',
icon: 'user',
},
{
code: 'B01',
permission_name: 'Create Batch',
groupBy: 'Batch',
icon: 'user',
},
{
code: 'B10',
permission_name: 'Update Batch',
groupBy: 'Batch',
icon: 'user',
},
];
Desired output:
Output = [
{
label: 'User',
icon: 'user',
children: [
{
label: 'Create Users',
},
{
label: 'Read All Users',
},
],
},
{
label: 'Batch',
children: [
{
label: 'Create Batchs',
},
{
label: 'Read All Batch',
},
{
label: 'Update Batch',
},
{
label: 'Disabled Batch',
},
],
},
];