HTML and CSS Reference
In-Depth Information
column
0
1
2
0
row
1
2
Figure 9.10
An array of three arrays.
Using the
Array
constructor, the following example is an array consisting of three
arrays.
var array_name=new Array(new Array(77,88,99),
new Array(50,60,99),
new Array(99,88,78)
);
or the literal and easier way:
var array_name= [ [77,88,99],
[50,60,99],
[99,88,78]
];
The first row is
array_name[0],
the first element in the first row is
array_name[0][0]
,
and the last element is
array_name[2][2]
. (JavaScript will not complain if the number of
elements in each of the nested arrays varies.)
EXAMPLE
9.8
<html>
<head><title>Two-dimensional array</title></head>
<body>
<table border="2" bordercolor="blue">
<caption>Grade Sheet</caption>
<tr>
<script type="text/javascript">
1
var grades= [ [77,88,99,75],
[50,60,99,89],
[99,88,78,92]
];
// alert(grades.length); Output is 3
// alert(grades);
Output is 77,88,99,75,50,60,99,89,
//
99,88,78,92