简体   繁体   English

动态数组名称javascript

[英]dynamic array names javascript

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

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。 Eval is not meant for this kind of dynamic evaluation and it is a huge performance hit. Eval不适用于这种动态评估,而且是一个巨大的性能影响。

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。 Eval is not meant for this kind of dynamic evaluation. Eval不适用于这种动态评估。 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. 如果你随机化或自动生成名称,没有概率。

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. 使用2D数组或对象等。 The purpose of this answer is just to show that it CAN be done, not that it SHOULD be done. 这个答案的目的只是为了表明它可以完成,而不是它应该完成。

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: 但是,如果topleftrightbottom都是相关的(引用同一个对象),则创建具有这些属性的对象并创建这些对象的单个数组可能更有意义:

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/ http://jsfiddle.net/UNuF8/

You missed, that Array has to be a String => "Array". 您错过了,Array必须是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 这是小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM