Neil Bradley
asked on
JQuery. Use a variable inside a switch statement
My aim is to add a variable inside a switch statement.
The variable "MyVar" is the result of a condition.
Snippet shown here with a couple of notes hopefully paints a better picture of my aim:
var searchType = "";
var dispalySearchType = "";
var divResult = "";
// we create the variable here
var MyVar = "ab_one";
if(settings.DisplayFltNoPrefix){
MyVar = "ab_two";
}
for (var i = 0; i < response.Result.length; i++) {
var result = response.Result[i];
var someFromTo = searchType === "From" ? result.ab_to : result.ab_from;
switch (result.abcl_flightstatus) {
case "Completed":
divResult += individualStatus.format(dispalySearchType,
someFromTo,
// here I need to echo the variable however no joy with this method
result.MyVar);
break;
default:
divResult += individualStatus.format(dispalySearchType,
someFromTo,
result.MyVar);
break;
}
}
// I tried to simply insert this but no luck
if(settings.DisplayFltNoPrefix){
result.ab_one;
}else{
result.ab_two;
}
Its clear I need a better way of declairing:
result.MyVar
so that "MyVar" echoes its variable as opposed to throing an error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for the feedback and assistance.
Aologies for the vagueness of my question however the final solution used a combination of this method of defining the var:
const myVar = settings.DisplayFltNoPrefix ? "ab_one" : "ab_two";
and wrapping [] around my var.
Thanks all.
... result["ab_two"] : result["ab_one"]
to stay with the more obvious bracket notation