JavaScript Tutorial/Array/Multi Dimensional Array — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:25, 26 мая 2010
Multi-dimensional array
The element of a multidimensional array is an array.
The inner array can be accessed by putting two [] operators.
<HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
function displayInventory(table) {
document.write("<TABLE BORDER=ON>");
document.write("<TH>Item Number</TH><TH>Item Name</TH><TH>Model Number</TH><TH>Quantity</TH>");
for(x=1; x<=3; x++) {
document.write("<TR><TD>",x,"</TD>");
for(y=0; y<=2; y++){
document.write("<TD>",table[x][y],"</TD>");
}
document.write("</TR>");
}
document.write("</TABLE>");
}
part1 = new Array("A","3",78);
part2 = new Array("B","2",45);
part3 = new Array("C","1","14");
brakeParts = new Array("",part1,part2,part3);
displayInventory(brakeParts);
-->
</SCRIPT>
</HTML>
Two-dimensional array work around
<HTML>
<HEAD>
<TITLE>
Two-dimensional array work around
</TITLE>
<HEAD>
<BODY>
<SCRIPT>
var matrix = new Array(4);
matrix[0] = new Array(4);
matrix[1] = new Array(4);
matrix[2] = new Array(4);
matrix[3] = new Array(4);
matrix[0][0] = "A1";
matrix[0][1] = "A2";
matrix[0][2] = "A3";
matrix[0][3] = "A4";
matrix[1][0] = "B1";
matrix[1][1] = "B2";
matrix[1][3] = "B3";
matrix[2][0] = "C1";
matrix[2][1] = "C2";
matrix[2][3] = "C3";
matrix[3][0] = "D1";
matrix[3][1] = "D2";
matrix[3][3] = "D3";
for (var i = 0; i < matrix.length; i++){
for (var j = 0; j < matrix.length; j++){
document.write ("Element (" + i + ", " + j + ") is " + matrix[i][j] + " -- ");
}
document.write("<br>");
}
</SCRIPT>
</BODY>
</HTML>