I have an empty object structured like so :
let users = {
"type1": {
"group1": {
"role1": {}
},
"group2": {
"role2": {},
"role3": {},
"role4": {}
},
},
"type2": {
"group1": {
"role1": {}
},
"group2": {
"role2": {}
},
"group3": {
"role3": {},
"role4": {}
}
}
};
I am then fetching my users with an associated type, group and role for each one. I would like to dynamically add my users to my object above in the right type/group/role combination as they are stored in variables.
I tried something like this but this is wrong:
fetchedUsers.forEach(user => {
users[user.type][user.group][user.role].push(user.email);
})
Is there a way to do this similar to what I tried ?
pushis an array method.Array#pushisObject.assignthough it doesn't modify the target object in place, so you will still need to do some reconstructive work.role: { name: 'Bob', role: 'admin' }where there is only one role assigned to it, orrole: [{ name: 'Bob', role: 'admin'}, { name: 'Jo', role: 'user' }]where there are many roles assigned to an array. How you want your output to look depends solely on what that structure is meant to look like. @Ryan