Elliott
Elliott

Reputation: 3864

setting javascript variable name from a variable

Hi is it possible to set a var name from another variable?

Such as I have the var test with different numbers each time the function is called so I want the number to be a variable name e.g. var test contains ' 1 ' so the new variable would be var 1. I then want to add a word to this number "word" to make the new variable become var 1word.

var newNumber = 1;
var newNumber += 'word';
       ^ becomes ' 1 '
the new var name becomes ' 1word '

edit:

function getNumber(newNumber) //  Gets a number, for this example say its the number '1' 
    {

        var newNumber += "something";  //The new varaible will be named "1something" (something can be any word)

    }

Hope I haven't made it sound too confusing, thanks.

Upvotes: 5

Views: 8531

Answers (8)

tta
tta

Reputation: 562

Far as I can understand, you want to create a variable that's named after the contents of another variable.

Not sure if you can do this directly, but you can do it pretty painlessly using objects.

var obj = new Object();
obj['original'] = 'name';
obj[obj]['original']] = 'another name';

The object now has this structure:

{
  'original': "name",
  'name': "another name" #variable named after original's contents
}

Upvotes: 3

Vitim.us
Vitim.us

Reputation: 22138

If you're using numeric "variables" like in your exemple (var 1; var 2;) you should use Array;

var list=[]; //declaration

list[1]="something"; //create a "variable" 1 with 'something' inside

list[2]="other thing"; //create another variable

var name=3;
var value="Hello";

list[name]=value; //works too

alert( list[1] ); //something

if you're using words as "keywords" you should use objects instead of Arrays

var list={}; //declaration

list["car1"]="Honda";
list["car2"]="Toyota";
list["fuit"]="Apple";

var name="greeting";
var value="Hello";

list[name]=value; //works too

alert( list["car2"] ); //Toyota

hope you understood.

Upvotes: 2

chamberlainpi
chamberlainpi

Reputation: 5241

You may be able to achieve this with the builtin method: eval( "your javascript code here" );

so maybe in your case it would be something like: eval( "var yourNewName = \"" + yourOldVarValue + "\""); then nullify your old variable.

But as the others express, I can't see why or in which case you would want to rename a variable (unless its for some weird hacking trickery lol).

[not tested, so this eval may not even work :S]

Upvotes: 0

danp
danp

Reputation: 15241

There's a simple way to achieve this, if you store all your variables inside an object/array. Remember, in javascript, arrays are objects, objects are objects etc :p

var mystuff = {}; // new object literal

var numVariables = 10;

for (var i = numVariables - 1; i >= 0; i--){
    var newkey          = i + "_my_index";
    mystuff[newkey]     = "some stuff";
}

Upvotes: 2

Ollie Edwards
Ollie Edwards

Reputation: 14739

Everything in Javascript is basically just an associative array. This means you can use array access notation and dot notation more or less interchangeably. Array access notation provides the feature you are looking for:

object["name"]="foo"

will set a name property on the given object.

If you want to create a global variable then use the window object

window["name"]="foo"

You can also use an expression or a variable in the square brackets

window[someVaribale]="foo";
window["name_" + someVariable]="foo";

Upvotes: 5

Plaudit Design
Plaudit Design

Reputation: 1156

We need more of an explanation of what you are trying to accomplish. This looks like a really bad idea. Most likely you really should just used a named index array. Eg.

var stuff = {}
stuff[1] = "one";
stuff["name"] = "david";

Upvotes: 1

Abe Miessler
Abe Miessler

Reputation: 85056

I'm confused by your question but it seems like you want to change a variables name which cannot be done. You can create a new variable with the desired name and assign it whatever value you want.

My question is why would you want to do this?

Upvotes: 0

SLaks
SLaks

Reputation: 887453

You can put the variables in an object and index the object using string keys.

For example:

var myData = { };
myData.x1 = 42;
alert(myData["x" + 1]);

You can also initialize the object using object notation:

var myData = { key: 42 };

Upvotes: 3

Related Questions