mspahr
mspahr

Reputation: 153

Dynamically building a complex object

This will be the different for every user based on their roles. I am looking to build an object that can look like this:

let permissions = {
    'state': {
        'tool': ['subTool1', 'subTool2']
    }
}

An example:

roles = ['NY_email_submit', 'NY_email_approve', 'NY_build_submit', 'NY_build_view', 'DC_email_submit']

let permissions = {
    'NY': {
      'email': ['submit', 'approve'],
      'build': ['submit', 'view']
    },
    'DC': {
      'email': ['submit']
    }
  };

I am looping through a list, named roles, passed in that contains strings broken up like state_tool_subTool.

I would like it to have no duplicates. For example, if the next user role ran through the loop with the object above is NY_build_approve, I would like to simply add approve to the the list at ['build'].

Currently I have this that is not working correctly.

roles.forEach(role => {
    role = role.split('_');

    let state = role[0];
    let tool = role[1];
    let subTool = role[2];

    if ([state] in permissions) {
      permissions[state] = { [`${tool}`]: [subTool] };
    } else {
      //permissions[state][`${tool}`].push(subTool);
    }
  });

Upvotes: 0

Views: 52

Answers (3)

Sven.hig
Sven.hig

Reputation: 4519

here is another approach using reduce

roles = ['NY_email_submit', 'NY_email_approve', 'NY_build_submit', 'NY_build_view', 'DC_email_submit']
sp=roles.map(o=>o.split("_")).reduce((acc,curr)=>{
        if (!acc[curr[0]]) acc[curr[0]]={...acc[curr[0]],[curr[1]]:[...[curr[2]]]}
        else { 
          if(acc[curr[0]][curr[1]]) {         
          i=acc[curr[0]][curr[1]]
          acc[curr[0]]={...acc[curr[0]],[curr[1]]:[...i,...[curr[2]]]} }
          else  {acc[curr[0]]={...acc[curr[0]],[curr[1]]:[...[curr[2]]]} }

        }
         return acc
},{})
console.log(sp)

Upvotes: 0

AJ_
AJ_

Reputation: 1485

This should do the trick! You were on the right track, just needed another layer of checks

let permissions = {};
  
roles = ['NY_email_submit','NY_email_approve','NY_build_submit','NY_build_view', 'DC_email_submit'];

roles.forEach(role => {
    let [state, tool, subTool] = role.split('_');

    if (state in permissions) {
      if (tool in permissions[state]) {
        permissions[state][tool].push(subTool)

      } else {
        permissions[state][tool] = [subTool]
      }
      
    } else {
      permissions[state] = {[tool]: [subTool]}
    }
});

console.log(permissions);

Upvotes: 1

Rinkesh Golwala
Rinkesh Golwala

Reputation: 1039

roles = ['NY_email_submit', 'NY_email_approve', 'NY_build_submit', 'NY_build_view', 'DC_email_submit']

let permissions = {};

roles.forEach(role => {
    role = role.split('_');

    let state = role[0];
    let tool = role[1];
    let subTool = role[2];

    if (!permissions[state]) { 
      permissions[state] = {[tool] : [subTool]};
    } else {
        if (permissions[state][tool]) {
          if(!permissions[state][tool].includes(subTool)) {
            permissions[state][tool] = [...permissions[state][tool], subTool];
          }
        }
        else {
          permissions[state][tool] = [subTool];
        }
    }
  });
console.log(permissions);

Upvotes: 1

Related Questions