i would like to be able to select a const
value using a variable. something like this (below) but without the horrible if/then
statement (or a switch/case statement)
const variableOne = 'this is variable one';
const variableTwo = 'this is variable two';
const variableThree = 'this is variable three';
let testVar = 'variableThree';
let result;
if ( testVar == 'variableOne' ) {
result = variableOne;
} else if testVar == 'variableTwo' ) {
result = variableTwo;
} else if testVar == 'variableThree' ) {
result = variableThree;
}
console.log(testVar);
yes an object would work, but i would really like to not have to change all the const
values:
let testVar = 'variableThree';
let myObj =
{ variableOne : 'this is variable one' },
{ variableTwo : 'this is variable two' },
{ variableThree : 'this is variable three'};
console.log(myObj[testVar]);
or even this, but i am still be changing the const
statements:
global['variableOne'] = 'this is variable one';
global['variableTwo'] = 'this is variable two';
global['variableThree'] = 'this is variable three';
let thisVar = 'variableThree';
console.log(global[thisVar]);
is there possibly a better way to do this? i cannot think of one. i thought i remembered reading something like this might work, but i forget the syntax.
const variableOne = 'this is variable one';
const variableTwo = 'this is variable two';
const variableThree = 'this is variable three';
let testVar = 'variableThree';
console.log( ${testVar} ); // this does not work, but hopefully it demonstrates the concept.
ohhh and NO eval()
statements - this would create a huge security issue i just assume not have to deal with.