0

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.

Andreas Wong
60.7k19 gold badges112 silver badges123 bronze badges
asked Apr 27, 2012 at 6:39
1
  • var myBuildingName[i] = [myBuildingName[i]]; Commented Apr 27, 2012 at 6:41

5 Answers 5

1

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];
answered Apr 27, 2012 at 6:43
Sign up to request clarification or add additional context in comments.

Comments

0

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]] = [];
}
answered Apr 27, 2012 at 6:46

Comments

0

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.

answered Apr 27, 2012 at 6:46

Comments

0

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'],...
answered Apr 27, 2012 at 6:47

Comments

0

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.

answered Apr 27, 2012 at 8:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.