Currently, I get the below output from the code,
[
{ module: 'subscriptions', action: [ 'create', 'edit' ] },
{ module: 'members', action: [ 'create' ] }
]
This is the expected output I want from the above output,
{
"subscriptions": {
"action": {
create,
edit
}
},
"members": {
"action: "{
create
}
}
}
This is the code I tried,
let output = [
{ module: 'subscriptions', action: [ 'create', 'edit' ] },
{ module: 'members', action: [ 'create' ] }
]
output =
output.map(({action, module}) => ({ [module]:action }) );
console.dir(output);
The above code currently gives me this wrong output,
[ { subscriptions: [ 'create', 'edit' ] }, { members: [ 'create' ] } ]
Could someone please help me to achieve the expected output?
>Solution :
By keeping an array for the nested properties, you could build new entries and then an object from it.
const
data = [{ module: 'subscriptions', action: [ 'create', 'edit' ] }, { module: 'members', action: [ 'create' ] }],
result = Object.fromEntries(data.map(({ module, action }) => [module, { action }]));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }