JavaScript Tutorial/Array/Array Declaration — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:25, 26 мая 2010

Array declaration with element count

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
var days_of_week = new Array(7);
days_of_week[0] = "Sunday";
days_of_week[1] = "Monday";
days_of_week[2] = "Tuesday";
days_of_week[3] = "Wednesday";
days_of_week[4] = "Thursday";
days_of_week[5] = "Friday";
days_of_week[6] = "Saturday";
var x = 2;
alert(days_of_week[x]);
//  -->
</script>
</head>
<body>
</body>
</html>


Define two arrays and use for statement to ouput their values

<html>
<head>
<title>Define two arrays and use for statement to ouput their values</title>
</head>
<body>
<h1>Define two arrays and use for statement to ouput their values</h1>
<table border="1">
<script language="javascript" type="text/javascript">
<!--
var myArray    = new Array();
    myArray[0] = "A";
    myArray[1] = "B";
    myArray[2] = "C";
    myArray[3] = "D";
    myArray[4] = "E";
var myArray2    = new Array();
    myArray2[0] = "1";
    myArray2[1] = "2";
    myArray2[2] = "3";
    myArray2[3] = "4";
    myArray2[4] = "5";
for (var i=0; i<myArray.length; i++) {
  document.write("<tr><td>" + myArray[i] + "</td>");
  document.write("<td>" + myArray2[i] + "</td></tr>");
}
//-->
</script>
</table>
</body>
</html>


Initialize a string array during declaration and check the array size

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
var days_of_week = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
alert(days_of_week.length);
//  -->
</script>
</head>
<body>
</body>
</html>


Integer Array Declaration with initialization

<html>
<head>
<title>Array Declaration</title>
<script language="javascript" type="text/javascript">
<!--
var x = new Array(2,3,4);
alert(x);
//-->
</script>
</head>
<body>
</body>
</html>


Output array element in a HTML table format

<html>
<head>
<title>Table of Phone Numbers</title>
</head>
<body>
<h1>Table of Phone Numbers</h1>
<table border="1">
<script language="javascript" type="text/javascript">
<!--
var myArray    = new Array();
    myArray[0] = "1";
    myArray[1] = "2";
    myArray[2] = "3";
    myArray[3] = "4";
    myArray[4] = "5";
for (var i=0; i<5; i++) {
  document.write("<tr><td>Number " + i + " is:</td>");
  document.write("<td>" + myArray[i] + "</td></tr>");
}
//-->
</script>
</table>
</body>
</html>


Paralled array

<html>
<head>
<title>Define two arrays and use for statement to ouput their values</title>
</head>
<body>
<h1>Define two arrays and use for statement to ouput their values</h1>
<table border="1">
<script language="javascript" type="text/javascript">
<!--
var myArray    = new Array();
    myArray[0] = "A";
    myArray[1] = "B";
    myArray[2] = "C";
    myArray[3] = "D";
    myArray[4] = "E";
var myArray2    = new Array();
    myArray2[0] = "1";
    myArray2[1] = "2";
    myArray2[2] = "3";
    myArray2[3] = "4";
    myArray2[4] = "5";
for (var i=0; i<myArray.length; i++) {
  document.write("<tr><td>" + myArray[i] + "</td>");
  document.write("<td>" + myArray2[i] + "</td></tr>");
}
//-->
</script>
</table>
</body>
</html>


Store strings in array

<html>
<head>
<title>Store strings in array</title>
</head>
<body>
<P>
<script language="javascript" type="text/javascript">
<!--
var myArray = new Array();
myArray[0] = "AAA";
myArray[1] = "BBB";
myArray[2] = "CCC";
for (var i=0; i<myArray.length; i++) {
  document.write("Element " +i+ " contains: " +myArray[i]+ "<br />");
}
//-->
</script>
</p>
</body>
</html>