sm21guy
sm21guy

Reputation: 626

find the variable which is equal to the value of another variable

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?

Upvotes: 0

Views: 856

Answers (6)

user405398
user405398

Reputation:

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

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532435

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] );

Upvotes: 1

Shalom Craimer
Shalom Craimer

Reputation: 21449

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.

Upvotes: 4

Michael
Michael

Reputation: 12802

Using window[var]:

one = 'two';

two = '1';

alert(window[one]); // Alerts: 1

Upvotes: 1

Matt Cashatt
Matt Cashatt

Reputation: 24208

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!

Upvotes: 0

gdoron
gdoron

Reputation: 150253

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

Upvotes: 0

Related Questions