JavaScript DHTML/Data Type/Two Dimensional Array
Содержание
Another way to declare the two-dimensional array
<html>
<head>
<title>Array literal</title>
<script type="text/javascript" >
var planets = [
["A", "B", "C"],
["D", "E", "F"]
];
for (var i = 0; i < planets.length; i++)
{
for (var j = 0; j < planets[i].length; j++)
{
document.write("planets[" + i + "][" + j + "] = " + planets[i][j]);
document.write("<BR>");
}
}
</script>
</head>
<body>
</body>
</html>
Sort two dimensional array
<html>
<head>
<title>Array literal</title>
<script type="text/javascript" >
function compare(a, b)
{
return a[1] - b[1];
}
var planets = [
["A", 0.3],
["B", 0.9],
["C", 1],
["D", 0.8],
["E", 2.4]
];
planets.sort(compare);
document.write(planets);
</script>
</head>
<body>
</body>
</html>
Use nested for loop to loop through the two-dimension array
<html>
<head>
<title>Array literal</title>
<script type="text/javascript" >
var planets = new Array(
new Array("A", "B", "C"),
new Array("D", "E", "F")
);
for (var i = 0; i < planets.length; i++)
{
for (var j = 0; j < planets[i].length; j++)
{
document.write("planets[" + i + "][" + j + "] = " + planets[i][j]);
document.write("<BR>");
}
}
</script>
</head>
<body>
</body>
</html>
Use two-dimensional array to store matrix data
<html>
<head>
<title>Array literal</title>
<script type="text/javascript" >
var planets = [
[1, "K", "F", "M", "1:45"],
[2, "N", "G", "M", "3.8"],
[3, "M", "A", "W", "8.4"],
[4, "F", "S", "R", "6.4"],
[5, "J", "C", "M", "6.6"]
];
for (var i = 0; i < planets.length; i++)
{
for (var j = 0; j < planets[i].length; j++)
{
document.write("planets[" + i + "][" + j + "] = " + planets[i][j]);
document.write("<BR>");
}
}
</script>
</head>
<body>
</body>
</html>