简体   繁体   English

多维数组中的特定数组元素

[英]Specific Array Element in Multidimensional Array

I have some data in the format of我有一些格式的数据

[[12, 23],[27,-6],[52, -32],[82, 11]]

How can I reference any specific element in these arrays?如何引用这些数组中的任何特定元素?

I know for a standard array such as我知道一个标准数组,例如

[a, b, c, d]

I could find b as arrayName[2]我可以找到 b 作为 arrayName[2]

Does such a method exist for referencing the nth element in the nth array in a multidimensional array?是否存在这样的方法来引用多维数组中第 n 个数组中的第 n 个元素? Also, if such a method exists, does it also apply to jagged arrays?另外,如果存在这样的方法,它是否也适用于锯齿状数组?

Does such a method exist for referencing the nth element in the nth array in a multidimensional array?是否存在这样的方法来引用多维数组中第 n 个数组中的第 n 个元素?

Yes:是的:

arrayName[x][y]

JavaScript doesn't have multi-dimensional arrays; JavaScript 没有多维数组; instead, it has arrays of arrays .相反,它有arrays of arrays 的数组 So what you have in your example is an array containing references to other arrays, so arrayName[x] gives us the reference to the array at position x of arrayName , then [y] gives us the element at position y of that array.因此,您在示例中拥有的是一个包含对其他数组的引用的数组,因此arrayName[x]为我们提供了对arrayName位置x处的数组的引用,然后[y]为我们提供了该数组位置y处的元素。

Also, if such a method exists, does it also apply to jagged arrays?另外,如果存在这样的方法,它是否也适用于锯齿状数组?

Yes, because there's nothing special about jagged/sparse arrays in JavaScript.是的,因为 JavaScript 中的锯齿状/稀疏数组没有什么特别之处。 JavaScript's standard arrays aren't arrays at all , in fact.事实上,JavaScript 的标准数组根本不是数组


Gratuitous Live Example:免费现场示例:

 var arrayName = [[12, 23],[27,-6],[52, -32],[82, 11]]; var x = 2; // The third array in 'arrayName' var y = 1; // The second entry in that array console.log(arrayName[x][y]);

Just specify a second index:只需指定第二个索引:

arrayName[1][1];

So, if you have arrayName = [[10, 20], [11, 25]] , arrayName[1] would be [11, 25] and arrayName[1][1] would be 25因此,如果您有arrayName = [[10, 20], [11, 25]] ,则arrayName[1]将为[11, 25]arrayName[1][1]将为25

We access the elements of a 2D array similarly to accessing a 1D array - by using the 0-based index value in both the row and column dimension with the array reference variable and bracket notation.我们访问二维数组的元素类似于访问一维数组 - 通过在行和列维度中使用基于 0 的索引值以及数组引用变量和括号表示法。 Look at the example below.看下面的例子。 Here is the payScaleTable reference variable from the example in the last section of this lesson.这是本课最后一节示例中的 payScaleTable 参考变量。 Following the reference table are two sets of brackets.参考表之后是两组括号。 The first set of brackets contains the index of the row that is to be accessed, and the second set of brackets contains the index of the column that is to be accessed.第一组括号包含要访问的行的索引,第二组括号包含要访问的列的索引。 The arrows in the figure show where the index-pair [2][1] "points" in the 2D array.图中的箭头显示了索引对[2][1]在二维数组中“指向”的位置。 It points to the third row (index 2) and the 2nd column (index 1).它指向第三行(索引 2)和第二列(索引 1)。 Essentially identical to 1D arrays, but now you have to think in 2D tabular format.本质上与一维数组相同,但现在您必须以二维表格格式思考。

二维数组图

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

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