admin管理员组

文章数量:1222242

I have a few arrays with like names.

ArrayTop[]  
ArrayLeft[]   
ArrayRight[]  
ArrayWidth[]

I am trying to set the name dynamically in a function and then set value.

I have tried many ways of dynamically picking the right array but have not come up with a solution.

function setarray(a,b,c){
    eval(Array+a+[b])=c
}

setarray('Top',5,100)

In this example i am trying to set.

ArrayTop[5]=100

I have a few arrays with like names.

ArrayTop[]  
ArrayLeft[]   
ArrayRight[]  
ArrayWidth[]

I am trying to set the name dynamically in a function and then set value.

I have tried many ways of dynamically picking the right array but have not come up with a solution.

function setarray(a,b,c){
    eval(Array+a+[b])=c
}

setarray('Top',5,100)

In this example i am trying to set.

ArrayTop[5]=100
Share Improve this question edited Mar 21, 2014 at 18:01 nanobash 5,5007 gold badges39 silver badges56 bronze badges asked Mar 21, 2014 at 17:58 user1410288user1410288 611 gold badge1 silver badge5 bronze badges 2
  • why don't u use an array of arrays? so you know is arr[0] bot is arr[1] and so on – Radu Commented Mar 21, 2014 at 18:01
  • 1 Or you could also use an object of arrays which would then allow you to have names for the arrays. – source.rar Commented Mar 21, 2014 at 18:02
Add a comment  | 

7 Answers 7

Reset to default 6

If you are doing this in the browser, one possible solution would be to do:

function setArray(a, b, c){
    window['Array' + a][b] = c;
}

setArray('Top', 5, 100);

I would recommend that all your array's be contained in some object and not pollute the global namespace. So it would be more like:

var arrays = {
    ArrayTop: [],
    ArrayNorth: []
};

function setArray(a, b, c){
    arrays['Array' + a][b] = c;
}

setArray('Top', 5, 100);

I would not recommend using eval. Eval is not meant for this kind of dynamic evaluation and it is a huge performance hit.

Hash map will be a perfect tool:

var arrays = {
  top: [],
  left: [],
  right: [],
  bottom: []
};

function addToArray(name, index, value) {
  arrays[name][index] = value;
}

addToArray('top', 5, 100);

I took the liberty to give more explicit names.

I suggest also two good practices:

  • do not use eval. Eval is not meant for this kind of dynamic evaluation. In your case, it's a performance killer

  • do not polute the global namespace. In browser environnement, avoid adding stuff to window (which is global).

Why not indexing your array with an object?

var arrayNames=["top","left","right","bottom"]
var data=[1,2,3,4,5];
var arrays={};

arrayNames.forEach(function(x){
    arrays[x]=data;
});    

So you could get your Array via Name. If you randomize or autogenerate the names, no prob.

Put all your arrays into an object:

var myArrays = { 
    top : arrayTop,
    left: arrayLeft,
    right: arrayRight,
    bottom: arrayBottom
}

And the to get an array you can just:

myArrays["top"][5] = 100;

Or you can skip defining the arrays as global variables and just do something like:

var myArrays = { 
    top : [],
    left: [],
    right: [],
    bottom: []
}

Which initializes 4 empty arrays which you can populate:

myArrays["top"][0] = 100;

or

myArrays.top[0] = 100;

However, if top, left, right and bottom all are related (refering to the same object), it might make more sense to create an object with those properties and create a single array of those objects:

function MyObj(top, left, right, bottom) {
   this.top = top;
   this.left = left;
   this.right = right;
   this.bottom = bottom;
}

var myArray = [];

myArray.push(new MyObj(1,2,3,4));
console.log(myArray[0]);

myArray[0].left = 7;
console.log(myArray[0]);

http://jsfiddle.net/UNuF8/

You can do it this way:

function setarray(a,b,c){
    window['Array' + a][b] = c;
}

setarray('Top',5,100)

However, you shouldn't be doing this. Use a 2D array or an object or something. The purpose of this answer is just to show that it CAN be done, not that it SHOULD be done.

You missed, that Array has to be a String => "Array". Then you can do

var postfix = "Top"; 
var a = eval("new Array"+postfix);
a.push(100);

try something like this

var someArr = {

};
someArr.ArrayTop = [];

function setarray(a, b, c) {

    var arrName = "Array" + a;
    someArr[arrName][b] = c;
}


setarray('Top', 5, 100)

alert(someArr.ArrayTop[5]);

Hope this works. Here is the fiddle

本文标签: dynamic array names javascriptStack Overflow