This question already has an answer here:
这个问题在这里已有答案:
- How can I create a two dimensional array in JavaScript? 40 answers
- 如何在JavaScript中创建二维数组? 40个答案
I would like to know how to declare 2 dimensional arrays in java script of fixed lengths something like,
我想知道如何在固定长度的java脚本中声明二维数组,如,
var array = new Array[this.rows, this.columns];
after that i would like to get the length of each dimension like,
之后,我想得到每个维度的长度,如,
array[0].length; // i am assuming this would give me the first dimension like in C# array.GetLength(0);
array[1].length; // i am assuming this would give me the second dimension like in C# array.GetLength(1);
any help is appreciated, thanks.
任何帮助表示赞赏,谢谢。
5 个解决方案
#1
31
A quick example of what @SLaks is referring to with jagged arrays. You basically put an array in an array. The below example shows one way to make an array thats 100x100.
@SLaks指的是锯齿状数组的快速示例。你基本上把数组放在一个数组中。下面的示例显示了一种制作100x100阵列的方法。
var arr = [];
for(var x = 0; x < 100; x++){
arr[x] = [];
for(var y = 0; y < 100; y++){
arr[x][y] = x*y;
}
}
console.log(arr[10][11]);
现场演示
This method is very flexible for instance arr[4]
could have an array indexed to 10, and arr[5]
could have an array with 1 value, or be even be a completely different type such as a string or number.
这种方法非常灵活,例如arr [4]可以有一个索引为10的数组,而arr [5]可以有一个数值为1的数组,或者甚至是一个完全不同的类型,如字符串或数字。
#2
3
Unlike C#, Javascript does not support multi-dimensional arrays.
与C#不同,Javascript不支持多维数组。
Instead, you can use nested ("jagged") arrays.
相反,您可以使用嵌套(“锯齿状”)数组。
#3
2
In javascript:
在javascript中:
var Matrix = function (rows, columns) {
this.rows = rows;
this.columns = columns;
this.myarray = new Array(this.rows);
for (var i=0; i < this.columns; i +=1) {
this.myarray[i]=new Array(this.rows)
}
return this.myarray;
}
var m = new Matrix(2,2);
m; // [[undefined, undefined], [undefined, undefined]];
var m2 = new Matrix(3,3);
m2; // [[undefined, undefined, undefined], [undefined, undefined, undefined], [undefined, undefined, undefined]]
#4
2
A nested 3x3 array (of undefined
):
嵌套的3x3数组(未定义):
var arr = new Array(3);
for (var i = 0; i < arr.length; ++i) {
arr[i] = new Array(3);
}
console.log(arr);
modle13 commented that you're restricted to a fixed size sub-array, so here's one quick tweak to get around that:
modle13评论说你被限制在一个固定大小的子阵列,所以这里有一个快速调整来解决这个问题:
var nestedSizes = [3,5,1,4];
var arr = new Array(nestedSizes.length);
for (var i = 0; i < arr.length; ++i) {
arr[i] = new Array(nestedSizes[i]);
}
console.log(arr);
#5
-3
could you not just do something like this?
你能不能做这样的事吗?
var x = new Array(4); #1st array, array of arrays
y = new Array(1, 4, 2, 0); #2nd dimension of arrays, an array of values
z = new Array(4, 8, 3, 9); #^^^
a = new Array(7, 0, 2, 4); #^^^
t = new Array(9, 0, 3, 1); #^^^
then to access 7 (the 1st value) in array a (3) you could type:
然后在数组a(3)中访问7(第1个值),您可以键入:
var example = x.3.1
if this wont work please tell me cos this is what i was told and what im now using to program my game
如果这不起作用请告诉我,这是我被告知的,以及我现在用来编程我的游戏
#1
31
A quick example of what @SLaks is referring to with jagged arrays. You basically put an array in an array. The below example shows one way to make an array thats 100x100.
@SLaks指的是锯齿状数组的快速示例。你基本上把数组放在一个数组中。下面的示例显示了一种制作100x100阵列的方法。
var arr = [];
for(var x = 0; x < 100; x++){
arr[x] = [];
for(var y = 0; y < 100; y++){
arr[x][y] = x*y;
}
}
console.log(arr[10][11]);
现场演示
This method is very flexible for instance arr[4]
could have an array indexed to 10, and arr[5]
could have an array with 1 value, or be even be a completely different type such as a string or number.
这种方法非常灵活,例如arr [4]可以有一个索引为10的数组,而arr [5]可以有一个数值为1的数组,或者甚至是一个完全不同的类型,如字符串或数字。
#2
3
Unlike C#, Javascript does not support multi-dimensional arrays.
与C#不同,Javascript不支持多维数组。
Instead, you can use nested ("jagged") arrays.
相反,您可以使用嵌套(“锯齿状”)数组。
#3
2
In javascript:
在javascript中:
var Matrix = function (rows, columns) {
this.rows = rows;
this.columns = columns;
this.myarray = new Array(this.rows);
for (var i=0; i < this.columns; i +=1) {
this.myarray[i]=new Array(this.rows)
}
return this.myarray;
}
var m = new Matrix(2,2);
m; // [[undefined, undefined], [undefined, undefined]];
var m2 = new Matrix(3,3);
m2; // [[undefined, undefined, undefined], [undefined, undefined, undefined], [undefined, undefined, undefined]]
#4
2
A nested 3x3 array (of undefined
):
嵌套的3x3数组(未定义):
var arr = new Array(3);
for (var i = 0; i < arr.length; ++i) {
arr[i] = new Array(3);
}
console.log(arr);
modle13 commented that you're restricted to a fixed size sub-array, so here's one quick tweak to get around that:
modle13评论说你被限制在一个固定大小的子阵列,所以这里有一个快速调整来解决这个问题:
var nestedSizes = [3,5,1,4];
var arr = new Array(nestedSizes.length);
for (var i = 0; i < arr.length; ++i) {
arr[i] = new Array(nestedSizes[i]);
}
console.log(arr);
#5
-3
could you not just do something like this?
你能不能做这样的事吗?
var x = new Array(4); #1st array, array of arrays
y = new Array(1, 4, 2, 0); #2nd dimension of arrays, an array of values
z = new Array(4, 8, 3, 9); #^^^
a = new Array(7, 0, 2, 4); #^^^
t = new Array(9, 0, 3, 1); #^^^
then to access 7 (the 1st value) in array a (3) you could type:
然后在数组a(3)中访问7(第1个值),您可以键入:
var example = x.3.1
if this wont work please tell me cos this is what i was told and what im now using to program my game
如果这不起作用请告诉我,这是我被告知的,以及我现在用来编程我的游戏