variables - Passing condition to a var in javascript -


i don't know how solve this. need this:

var condition; if (a==1) //a comes part     condition = "arr3[cliente].año == year"; if (a==2)     condition = "arr3[cliente].año == year && arr3[cliente].sic"  //now if  if (condition){       //rest of code } 

i need different conditions depending previous values, code inside last if same, don't need:

if (arr3[cliente].año == year)     // code else if (arr3[cliente].año == year && arr3[cliente].sic)     // code 

how can it?

just assign result of expressions variable (currently assigning string). expression doesn't have inside if statement, result what's important:

var condition; if (a==1)     condition = arr3[cliente].año == year; if (a==2)     condition = arr3[cliente].año == year && arr3[cliente].sic;  // point `condition` either have value `undefined`, `true` or `false`.     if (condition) {   // code } 

of course can simplify/reduce following:

if (arr3[cliente].año == year && (a == 1 || == 2 && arr3[cliente].sic)) {   // code } 

no need repeating if statements or comparisons. assumes accessing of properties doesn't have side effects.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -