I was reading a book about Javascript and saw this line;
我正在读一本关于Javascript的书,看到了这一行;
JavaScript does not support true multidimensional arrays, but you can approximate them with arrays of arrays.
JavaScript不支持真正的多维数组,但您可以使用数组数组来近似它们。
What's the difference?
有什么不同?
5 个解决方案
#1
17
A true multi-dimensional array must be indexed with multiple indices. An array of arrays can be indexed with a single index, which will return another array. A true multi-dimensional array stores all its data contiguously. An array of arrays stores all its constituent arrays arbitrarily scattered around. This can improve iteration performance because of cache effects for true arrays.
必须使用多个索引对真正的多维数组进行索引。可以使用单个索引索引数组数组,该索引将返回另一个数组。真正的多维数组连续存储其所有数据。数组数组存储任意散布的所有组成数组。由于真实数组的缓存效应,这可以提高迭代性能。
#2
3
(a visual explanation that complements an excellent answer of @recursive)
(一个视觉解释,补充了@recursive的优秀答案)
In some languages (C#) there are both. The difference is in "shape" of such arrays.
在某些语言(C#)中都有。不同之处在于此类阵列的“形状”。
int[3, 4] // true two-dimensional array
// it will "look" like this, rectangular shape
[[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]]
But when you define an array of arrays, it can easily (especially in javascript) look like this. It's called a jagged array.
但是当你定义一个数组数组时,它很容易(特别是在javascript中)看起来像这样。它被称为锯齿状阵列。
[[0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0]]
#3
2
There is nothing in JavaScript like...
JavaScript中没有任何内容......
var arr = new Array[5][3]; /* Some weird JS/C-like thing for demonstration */
...with enforced lengths. Like a matrix.
......具有强制性的长度。就像一个矩阵。
However, you can build an Array
of which its members are all Array
s of a fixed length. If one of the sub Array
s were to have a different length, it would be a jagged Array
.
但是,您可以构建一个Array,其成员都是固定长度的数组。如果其中一个子阵列具有不同的长度,则它将是锯齿状阵列。
#4
2
The author seems to assume that a “true” multidimensional array is one where all elements:
作者似乎假设“真正的”多维数组是所有元素的数组:
-
Are arrays;
是数组;
-
Are stored by value, not by reference; and
是按值存储,而不是通过引用存储;和
-
Have the same length.
长度相同。
JavaScript arrays may contain other arrays, but not by value, only by reference. That is, the elements of each row may be contiguous in memory, but the rows themselves may not. Further, there is no way to statically indicate that all of the inner arrays must have the same length, because JavaScript is dynamically typed.
JavaScript数组可能包含其他数组,但不包含值,仅通过引用。也就是说,每行的元素在存储器中可以是连续的,但行本身可以不是。此外,没有办法静态地指示所有内部数组必须具有相同的长度,因为JavaScript是动态类型的。
But an array of arrays is precisely what you ought to use to represent a multidimensional array in JavaScript, and the details of the internal representation are probably not relevant to you when you’re just learning the language.
但是,数组数组正是您应该用来表示JavaScript中的多维数组的内容,当您刚刚学习语言时,内部表示的细节可能与您无关。
#5
1
Although the JavaScript specification (3.0) does not make mention of multi-dimensional arrays, they are in fact possible. Multi-dimensional arrays can be represented by arrays of arrays. See.
虽然JavaScript规范(3.0)没有提到多维数组,但它们实际上是可行的。多维数组可以由数组数组表示。看到。
For example.
例如。
var items = [[1,2],[3,4],[5,6]];
alert(items[0][0]); // Would alert 1
One dimensional array in Javascript.
Javascript中的一维数组。
var a = [0, 1, 2, 3];
a[[2]] === a[2]; // this is true
2 == [2] //true
// Even complex
2 == [[[2]]] //true
// And even more
var a = { "xyz" : 1 };
a[[[["xyz"]]]] === a["xyz"]; //true
#1
17
A true multi-dimensional array must be indexed with multiple indices. An array of arrays can be indexed with a single index, which will return another array. A true multi-dimensional array stores all its data contiguously. An array of arrays stores all its constituent arrays arbitrarily scattered around. This can improve iteration performance because of cache effects for true arrays.
必须使用多个索引对真正的多维数组进行索引。可以使用单个索引索引数组数组,该索引将返回另一个数组。真正的多维数组连续存储其所有数据。数组数组存储任意散布的所有组成数组。由于真实数组的缓存效应,这可以提高迭代性能。
#2
3
(a visual explanation that complements an excellent answer of @recursive)
(一个视觉解释,补充了@recursive的优秀答案)
In some languages (C#) there are both. The difference is in "shape" of such arrays.
在某些语言(C#)中都有。不同之处在于此类阵列的“形状”。
int[3, 4] // true two-dimensional array
// it will "look" like this, rectangular shape
[[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]]
But when you define an array of arrays, it can easily (especially in javascript) look like this. It's called a jagged array.
但是当你定义一个数组数组时,它很容易(特别是在javascript中)看起来像这样。它被称为锯齿状阵列。
[[0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0]]
#3
2
There is nothing in JavaScript like...
JavaScript中没有任何内容......
var arr = new Array[5][3]; /* Some weird JS/C-like thing for demonstration */
...with enforced lengths. Like a matrix.
......具有强制性的长度。就像一个矩阵。
However, you can build an Array
of which its members are all Array
s of a fixed length. If one of the sub Array
s were to have a different length, it would be a jagged Array
.
但是,您可以构建一个Array,其成员都是固定长度的数组。如果其中一个子阵列具有不同的长度,则它将是锯齿状阵列。
#4
2
The author seems to assume that a “true” multidimensional array is one where all elements:
作者似乎假设“真正的”多维数组是所有元素的数组:
-
Are arrays;
是数组;
-
Are stored by value, not by reference; and
是按值存储,而不是通过引用存储;和
-
Have the same length.
长度相同。
JavaScript arrays may contain other arrays, but not by value, only by reference. That is, the elements of each row may be contiguous in memory, but the rows themselves may not. Further, there is no way to statically indicate that all of the inner arrays must have the same length, because JavaScript is dynamically typed.
JavaScript数组可能包含其他数组,但不包含值,仅通过引用。也就是说,每行的元素在存储器中可以是连续的,但行本身可以不是。此外,没有办法静态地指示所有内部数组必须具有相同的长度,因为JavaScript是动态类型的。
But an array of arrays is precisely what you ought to use to represent a multidimensional array in JavaScript, and the details of the internal representation are probably not relevant to you when you’re just learning the language.
但是,数组数组正是您应该用来表示JavaScript中的多维数组的内容,当您刚刚学习语言时,内部表示的细节可能与您无关。
#5
1
Although the JavaScript specification (3.0) does not make mention of multi-dimensional arrays, they are in fact possible. Multi-dimensional arrays can be represented by arrays of arrays. See.
虽然JavaScript规范(3.0)没有提到多维数组,但它们实际上是可行的。多维数组可以由数组数组表示。看到。
For example.
例如。
var items = [[1,2],[3,4],[5,6]];
alert(items[0][0]); // Would alert 1
One dimensional array in Javascript.
Javascript中的一维数组。
var a = [0, 1, 2, 3];
a[[2]] === a[2]; // this is true
2 == [2] //true
// Even complex
2 == [[[2]]] //true
// And even more
var a = { "xyz" : 1 };
a[[[["xyz"]]]] === a["xyz"]; //true