I came across a confusing fact during coding a script.
How can i find the variable which is equal to the value of another variable. THEN , get the value of that variable.
Here’s an example:
var result;
var 1 = "john";
var 2 = "amy";
var 3 = "micheal";
var info = "1";
When var info is set to 1 , the script will then look for variable 1 which has the value JOHN then get the value john . Then set the result’s value to “john”.
For the same thing ,
When var info is set to 2 , the script will then look for variable 2 which has the value AMY then get the value amy . Then set the result’s value to “amy”.
and so on..
My info variable’s value is not determined. it could be 1 , 2 or 3 which is set/determined by an user event.
P/S i can use if and else , but i want to know how this can be done. 🙂
So how can i do this?
You probably want to do something very different:
That will put ‘john’ into
result.You see, this defines something called a “lookup table”, and saves it into
names. Then, you can use a “key” (in your case: 1, 2, 3) to look up a value. Writingnames[info]looks insideinfo, and gets its value, which in the above example is"1". It then looks for the key"1"insidenames, and sees that the value for"1"is'john'.I know this isn’t exactly what you were asking about, but I suspect it might help.