0

I had the following suggestion on how to set the value of a field dynamically:

Can I set the value of a property dynamically?

var data = {roles: "Admin:Xxxx:Data"};
var user = {data: {role:{}}};

data.roles.split(':').forEach(function(v) {
  user.data.role['is' + v] = true; 
})

This works very well for me.

Now I need to set the value of another parameter based on the presence or absence of different roles. The formula is

index = 0 if the Test role is present
index = index + 1 if the Xxxx role is present
index = index + 2 if the Yyyy role is present
index = index + 100 if the Data role is present 

I would appreciate some advice on how I could combine this into the solution that I have. If possible I would like to make the solution so that it could extended with more roles later on. I would like to have to avoid writing multiple if statements if possible.

Community
  • 1
  • 1

1 Answers1

1

use an object.

var indexes = {'Xxxx':0,'Yyyy':index+1,'Data':index+100};

index=indexes['Xxxx'];
el3ien
  • 5,362
  • 1
  • 17
  • 33