最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access value in multidimensional array? - Stack Overflow

programmeradmin1浏览0评论

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; there's no alert. When I change the inside of alert to a string, such as 'test', however, then the alert box does e up.

What am I doing wrong? How can I get the value of an item in a multidimensional array?

Thanks!

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; there's no alert. When I change the inside of alert to a string, such as 'test', however, then the alert box does e up.

What am I doing wrong? How can I get the value of an item in a multidimensional array?

Thanks!

Share Improve this question asked Jun 24, 2012 at 0:31 NateNate 28.6k39 gold badges136 silver badges228 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

The first line of your code:

var myArray = new Array();

...will create a new, single dimensional array, myArray, that has no elements. 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].

That's why you have to to assign myArray[0] to refer to a new array before you can access myArray[0][0]. The same thing applies to myArray[1], because JavaScript doesn't have multi-dimensional arrays per se, it has arrays of arrays. 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().

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.)

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. 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.

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"
发布评论

评论列表(0)

  1. 暂无评论