使用循环javascript的多变量声明

时间:2022-11-15 22:33:48

I was trying to create an array which contains 3 fixed fields.
So, something like

我试图创建一个包含3个固定字段的数组。所以,像

var x0 = new Array(x,y,z);

Now the thing is there can be multiple arrays like x[], which have the different values.
So something like

现在的问题是可以有多个像x []这样的数组,它们具有不同的值。所以像

   var x1 = new Array(x,y,z); x2 = new Array(x,y,z);

I would be passing values to the variables x,y,z like x0[0] = "test", x0[1] = "another",...
I need to create a variable number of such arrays, so say if i pass a value say 10, it should create 10 different arrays for me. How does one do this, is it allowed to use a loop for creating variables?

我会将值传递给变量x,y,z,如x0 [0] =“test”,x0 [1] =“另一个”,...我需要创建一个可变数量的此类数组,所以说如果我传递一个值10,它应该为我创建10个不同的数组。如何做到这一点,是否允许使用循环来创建变量?

There is something called as multi dimensional array, but I wasn't sure of how to use it. I tried but apparently it is very different and I did not understand it. Also, I thought of declaring an object, and passing values to it's parameters, and create objects as needed. Is this possible? And which method will be better?

有一种称为多维数组的东西,但我不知道如何使用它。我试过,但显然它是非常不同的,我不理解它。此外,我想到声明一个对象,并将值传递给它的参数,并根据需要创建对象。这可能吗?哪种方法会更好?

Is there any other way of doing this? maybe something which would be most efficient?

有没有其他方法这样做?也许最有效率的东西?

3 个解决方案

#1


2  

Just create an array of arrays:

只需创建一个数组数组:

var myArrays = [];

for(var i=0;i<something;i++){
    myArrays.push([x,y,z]);
}

Then, instead of x0[0] you would have x[0][0].

然后,你将得到x [0] [0]而不是x0 [0]。

Note, you shouldn't use new Array but instead use [].

请注意,您不应使用新数组,而应使用[]。

#2


1  

Do you mean

你的意思是

var x  =[];
x[0]=["x","y","z"];
x[1]=["x","y","z"];
x[2]=["x","y","z"];

or

要么

function addArr(arr,newarr) {
  arr[arr.length]=newarr; // or arr.push(newarr);
}
var x  =[];
for (var i=0;i<10;i++) {
  addArr(x,[i,i+1,i+2]);
}

#3


1  

var n = 100; // number of x-arrays (x0, x1, ...)
var allX = new Array(n); // array of length n

for (var i=0; i<n; i++){
   allX[i] = [x,y,z]; // array with elements x, y, z
}

now you can access e.g. the second array (x1) like:

现在您可以访问例如第二个数组(x1)如:

var x1 = allX[1];

or the third element of the third array (x2[2]) like:

或者第三个数组的第三个元素(x2 [2])如:

var z = allX[2][2];

Edit: see this explanation about the differences of array declaration between new Array() and []

编辑:请参阅有关新Array()和[]之间数组声明差异的说明

#1


2  

Just create an array of arrays:

只需创建一个数组数组:

var myArrays = [];

for(var i=0;i<something;i++){
    myArrays.push([x,y,z]);
}

Then, instead of x0[0] you would have x[0][0].

然后,你将得到x [0] [0]而不是x0 [0]。

Note, you shouldn't use new Array but instead use [].

请注意,您不应使用新数组,而应使用[]。

#2


1  

Do you mean

你的意思是

var x  =[];
x[0]=["x","y","z"];
x[1]=["x","y","z"];
x[2]=["x","y","z"];

or

要么

function addArr(arr,newarr) {
  arr[arr.length]=newarr; // or arr.push(newarr);
}
var x  =[];
for (var i=0;i<10;i++) {
  addArr(x,[i,i+1,i+2]);
}

#3


1  

var n = 100; // number of x-arrays (x0, x1, ...)
var allX = new Array(n); // array of length n

for (var i=0; i<n; i++){
   allX[i] = [x,y,z]; // array with elements x, y, z
}

now you can access e.g. the second array (x1) like:

现在您可以访问例如第二个数组(x1)如:

var x1 = allX[1];

or the third element of the third array (x2[2]) like:

或者第三个数组的第三个元素(x2 [2])如:

var z = allX[2][2];

Edit: see this explanation about the differences of array declaration between new Array() and []

编辑:请参阅有关新Array()和[]之间数组声明差异的说明