javascript 动态数组名称javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22566160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamic array names javascript
提问by user1410288
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
采纳答案by tyronegcarter
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.
我不建议使用 eval。Eval 并不适用于这种动态评估,它会造成巨大的性能损失。
回答by Feugy
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).
不要使用评估。Eval 不适用于这种动态评估。在你的情况下,它是一个性能杀手
不要污染全局命名空间。在浏览器环境中,避免向窗口(这是全局的)添加内容。
回答by Thomas Junk
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.
所以你可以通过名称获取你的数组。如果您随机化或自动生成名称,则没有问题。
回答by Abhishek Prakash
回答by xbonez
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.
但是,您不应该这样做。使用二维数组或对象或其他东西。这个答案的目的只是为了表明它可以完成,而不是它应该完成。
回答by Matt Burland
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:
它初始化了 4 个您可以填充的空数组:
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:
但是,如果top
, left
,right
和bottom
all 相关(指的是同一个对象),则创建具有这些属性的对象并创建这些对象的单个数组可能更有意义:
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]);
回答by Stephan
You missed, that Array has to be a String => "Array". Then you can do
您错过了,该 Array 必须是 String => "Array"。然后你可以做
var postfix = "Top";
var a = eval("new Array"+postfix);
a.push(100);