Sudeep
Sudeep

Reputation: 119

How do i create variable names using contents of an array using javascript

i have created an array,

var myBuildingName=['A1','A2','A3','A4'];

where A1,A2,A3 and A4 are the names obtained through user input. i now want to create arrays that have names A1,A2,A3 and A4.

i have tried using

for(var i=0;i<myBuildingName.length;i++)
{
   var myBuildingName[i]=[];
}

but it does not work...

please help.

Upvotes: 0

Views: 640

Answers (5)

rkw
rkw

Reputation: 7297

You have received a lot of great answers, if one of them serves your need, you should accept that answer. The fact that this is still open makes us think no-one has quite answered your question the way you had hoped.

If that is the case, I can only assume that you wanted to use those variables in a global scope.

var myBuildingName=['A1','A2','A3','A4'];

for (var i = 0; i < myBuildingName.length; i++) {
   window[myBuildingName[i]] = [];
}

Now you can access your variables 'normally'.

A1.push('test');

Note: this is horrible practice, since you should never pollute the global space.

Upvotes: 0

Joseph
Joseph

Reputation: 119837

Here's a demo

var myBuildingName = ['A1', 'A2', 'A3', 'A4'];

function arrayFromNames(arr) {
    var store = {};                         //storage for the arrays
    for (var i = 0; i < arr.length; i++) {  //for each in the passed names
        store[arr[i]] = [];                 //add to the storage an array with the corresponding name
    }
    return store;                           //return the storage
}

var nameArrays = arrayFromNames(myBuildingName);  //build using your array

console.log(nameArrays);​

//you now have:
//nameArrays.A1, nameArrays.A2,...
//or
//nameArrays['A1'], nameArrays['A2'],...

Upvotes: 0

RobG
RobG

Reputation: 147363

You can't access the local variable object (except in global code), so you can't add properties other than by variable declaration. For global code in the global context you could do:

var global  = this;
for ( ...) {
  global[myBuildingName[i]] = []
}

but you can't do that for function code in function context. See jfriend00's answer.

Upvotes: 0

Nadh
Nadh

Reputation: 7243

If you actually wanto create variables with those names (which I won't recommend), you'd have to eval() them. So:

for(var i=0;i<myBuildingName.length;i++)
{
   eval("var " + myBuildingName[i] + " = [] "); // This creates Array variables called A1, A2 etc.
}

Again, the above method is NOT recommended. You should assign the names as keys to an object literal, like:

var myStuff = {};
for(var i=0;i<myBuildingName.length;i++)
{
   var myStuff[myBuildingName[i]] = [];
}

Upvotes: 0

jfriend00
jfriend00

Reputation: 707238

You create a master parent object and use the array values as keys into the object where you can store an array for each.

var myBuildingName=['A1','A2','A3','A4'];

var master = {};

for (var i = 0; i < myBuildingName.length; i++) {
   master[myBuildingName[i]] = [];
}

Then, you can access the data like:

var a1Array = master['A1'];

or

var firstA1Item = master['A1'][0];

Upvotes: 1

Related Questions