javascript - How to use a multi-variable string in the name of a variable in pure js -


i'm huge js nub , still getting used syntactical nuances. essentially, i'm looking reduce following single line:

if (player==1) {variable1[arrayposition] = thisvalue;}  if (player==2) {variable2[arrayposition] = thisvalue;} 

you can use ternary operator define "variable" use and, assuming array position , value same use on 1 line:

(player === 1 ? variable1 : variable2)[arrayposition] = thisvalue; 

i assuming player can equal 1 or 2. if have to, should null check or check other values, should elsewhere or additionally.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -