简体   繁体   English

如何访问多维数组中的值?

[英]How to access value in multidimensional array?

I have the following code: 我有以下代码:

<html>
<body>
<div style="background-color: lightblue;" onClick="alert(myArray[0][1])">
this is a div
</div>

<script type="text/javascript">
var myArray = new Array();
myArray[0][0] = 0;
myArray[0][1] = 00012;
myArray[0][2] = 00006;
myArray[1][0] = 1;
myArray[1][1] = 00004;
myArray[1][2] = 00001;
</script>
</body>
</html>

When I click on the div, nothing happens; 当我单击div时,什么也没有发生。 there's no alert. 没有警报。 When I change the inside of alert to a string, such as 'test', however, then the alert box does come up. 但是,当我将警报的内部更改为字符串(例如“ test”)时,警报框就会出现。

What am I doing wrong? 我究竟做错了什么? How can I get the value of an item in a multidimensional array? 如何获取多维数组中项目的值?

Thanks! 谢谢!

Write it out like this: 像这样写出来:

<div style="background-color: lightblue;" onClick="alert(myArray[0][1])">
    this is a div
</div>

<script type='text/javascript'>
    var myArray = [];
    myArray.push([0, 00012, 00006]);
    myArray.push([1, 00004, 00001]);
</script>

Edit 编辑

The problem is that when you write this: 问题是当您编写此代码时:

var myArray = new Array();
myArray[0][0] = 0;

The first item in myArray is undefined, so you can't do anything with it. myArray中的第一项是未定义的,因此您无法对其进行任何操作。 Using this method, you'd have to create the array first: 使用此方法,您必须首先创建数组:

var myArray = new Array();
myArray[0] = new Array();
myArray[0][0] = 0;

But I think the method of using the square notation with push is cleaner. 但是我认为将方形符号与push一起使用的方法更加简洁。

This is how you declare a multi dimensional array: 这是您声明多维数组的方式:

MultiArray = new Array(2)

MultiArray [0] = new Array(2)

MultiArray [0][0] = "Tom"

MultiArray [0][1] = "scientist"

MultiArray [1] = new Array(2)

MultiArray [1][0] = "Beryl"

MultiArray [1][1] = "engineer"

The first line of your code: 您的代码的第一行:

var myArray = new Array();

...will create a new, single dimensional array, myArray , that has no elements. ...将创建一个没有元素的新的一维数组myArray Then when you say: 然后当你说:

myArray[0][0] = 0;          

...you are trying to access a dimension that doesn't exist yet. ...您正在尝试访问一个尚不存在的维度。 That is, myArray[0] is undefined because although myArray is an array it doesn't have any elements yet - so myArray[0][0] is like saying undefined[0] . 也就是说, myArray[0]undefined因为尽管myArray是一个数组,但还没有任何元素-所以myArray[0][0]就像说undefined[0]

That's why you have to to assign myArray[0] to refer to a new array before you can access myArray[0][0] . 这就是为什么在访问myArray[0][0]之前必须分配myArray[0]来引用新数组。 The same thing applies to myArray[1] , because JavaScript doesn't have multi-dimensional arrays per se, it has arrays of arrays. 同样的情况也适用于myArray[1] ,因为JavaScript本身没有多维数组,因此具有数组数组。 So this is what you need (for a minimal change to your existing code): 因此,这就是您所需要的(对现有代码的最小更改):

var myArray = [];
myArray[0] = [];
myArray[0][0] =  00012;
myArray[0][1] = 00012;
myArray[0][2] = 00006;
myArray[1] = [];
myArray[1][0] = 1;
myArray[1][1] = 00004;
myArray[1][2] = 00001;

Note that [] is equivalent to new Array() . 注意[]等同于new Array()

An easier to read and type option is to create the sub-arrays via array literal syntax: 易于阅读和键入的选项是通过数组文字语法创建子数组:

var myArray = [];
myArray[0] = [00012, 00012, 00006];
myArray[1] = [1, 00004, 00001];

Or, easiest of all (especially if these are hard-coded values) is creating the whole thing in one statement via a nested array literal (white-space is ignored): 或者,最简单的方法(尤其是如果这些是硬编码的值)是通过嵌套数组文字(忽略空格)在一个语句中创建整个内容:

var myArray = [
                [00012, 00012, 00006],
                [1, 00004, 00001]
              ];

(Note also that those leading zeros will disappear for numeric data: use strings ( "00012" instead of 00012 ) if you want to retain the zeros.) (还请注意,对于数字数据,这些前导零将消失:如果要保留零,请使用字符串( "00012"而不是00012 )。)

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

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