Saturday, 15 March 2014

How can I set the value of and index parameter within a .forEach loop in Javascript? -



How can I set the value of and index parameter within a .forEach loop in Javascript? -

i had next suggestion on how set value of field dynamically:

can set value of property dynamically?

var info = {roles: "admin:xxxx:data"}; var user = {data: {role:{}}}; data.roles.split(':').foreach(function(v) { user.data.role['is' + v] = true; })

this works me.

now need set value of parameter based on presence or absence of different roles. formula

index = 0 if test role nowadays index = index + 1 if xxxx role nowadays index = index + 2 if yyyy role nowadays index = index + 100 if info role nowadays

i appreciate advice on how combine solution have. if possible create solution extended more roles later on. have avoid writing multiple if statements if possible.

use object.

var indexes = {'xxxx':0,'yyyy':index+1,'data':index+100}; index=indexes['xxxx'];

javascript

No comments:

Post a Comment