0

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?

sm21guy
  • 626
  • 3
  • 13
  • 33

6 Answers6

4

You probably want to do something very different:

var result;

var names = {
  '1': 'john',
  '2': 'amy',
  '3': 'michael'
};

var info = "1";
result = names[info];

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. Writing names[info] looks inside info, and gets its value, which in the above example is "1". It then looks for the key "1" inside names, 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.

Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106
  • If you are only using numbers, you can use an array instead: `var names = ['john', 'amy', 'michael'];`. – Dennis Mar 25 '12 at 13:16
  • Yup! Just remember that if you use an array, then the first item is automatically `0`, the second is `1`, the third is `2`, etc. – Shalom Craimer Mar 25 '12 at 14:53
1

Numbers aren't legal variable names, but you can use a bit of evil to do what you want: http://jsfiddle.net/2mAeh/2/ (only tested in Safari). Just to be clear, I don't recommend this. It is evil.

var result;
var a = "amy";
var b = "john";
var c = "micheal";
var info = "a";

var variable = eval("info"); // just to satisfy your requirement, same as info
result = eval(variable);

alert(variable + ' = ' + result);

A better way to handle this would be with a map, using your "variable names" as the keys. In this case you could use numbers as the keys, but I'll use the same keys as my other example for consistency.

var map = { "a": "Amy", "b" : "John", "c" : "micheal" };
var info = "a";

alert( info + ' = ' + map[info] );
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

Using window[var]:

one = 'two';

two = '1';

alert(window[one]); // Alerts: 1
Michael
  • 11,912
  • 6
  • 49
  • 64
0

So how can i do this?

You can NOT.(out of the box)
You can not know the variables names only their values.

If you really wants to do it, and wanna pay it with a spaghetti code, take a look at this:
Get variable name. javascript "reflection"

or use eval as suggested by @tvanfosson to get the variable value, which is dangerous and deprecated.


  • Note that variables in javascript can't begin with a number. var 1 = "john"; => invalid

javscript variables names rules

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
0

Like this,

function findVariableByName(info){// info is 1, 2, 3

var result;
this['1'] = "john";
this['2'] = "amy";
this['3'] = "micheal";
result = this[info];
alert(result);
return result;
}

findVariableByName(1);

Update:

If you change your code like below and if it is in global scope,

var result; var 1 = "john"; var 2 = "amy"; var 3 = "micheal"; var info = "1";

var one = 'john';
var two = 'amy';
var three = 'micheal';
var info = 'one';

You can do,

window[info] // gives you john
0

You do this by setting up an object:

// Store your object
var obj = {result: null, a: "john", b: "amy", c: "michael", info: "1"}

// Iterate like so.  
    for(var key in obj) {
        if(obj[key] == info){
          alert(key);
        }
    }

I have not checked this code but it should work. Good luck!

Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111