Array Constructors:


var variable = new Array()
var variable = new Array(int)
var variable = new Array(arg1, ..., argN)
These constructors create a new array and initialize the Array object based on the arguments passed in the parameter list.
The constructor that has no arguments sets the length property to 0.
int– An array is created and its length property is set to the value int.
arg1,…argN–An array is created and the array is populated with the arguments. The array length property is set to the number of arguments in the parameter list.
The newly created array is returned from the constructor
Use Array’s Constructor

Arrays stores multiple data based on a numbered position into one storage structure.
The index starts at 0 and goes up.
JavaScript supports having arrays within arrays, called multidimensional arrays.
One-Dimensional array
To create an instance of an array, you use the new operator along with the Array object.
There are four ways to declare an array.
First, an empty array can be created by leaving the constructor parameters empty:
var x = new Array();
The second way to create an array is to fill in the constructor parameters with the array elements.
An array can contain elements of various types:
var x = new Array("A","BB","CCC",1,5,8);
The third way to create an array is to fill in the constructor parameter with the size of the array.
This initializes the array to hold the number of elements specified, but does not specify the actual elements.
var x = new Array(6);
The fourth way to create an array is to use the array square brackets to fill in the array elements directly.
var x = ["A","BB","CCC",1,5,8];

 Creating an Array and Accessing Its Elements:
<html>
<body>
<h2>Creating and Accessing Arrays</h2>
<script language="JavaScript">
<!--
numArray = new Array(4,6,3);

document.write("[0]=",numArray[0],"<br>");
document.write("[1]=",numArray[1],"<br>");
document.write("[2]=",numArray[2]);
//End Hide-->
</script>
</body>
</html>

Create an array using the Object() constructor:

<html>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var myArray = Object();
myArray.length=3;   //array position zero
myArray[1]="A";
myArray[2]="B";
myArray[3]="C";

document.write("The myArray holds:  ");

for(x=1; x<=myArray.length; x++) {
document.write(myArray[x],"  ");
}
-->
</SCRIPT>
</body>
</html>

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>
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>
Define two arrays and use for statement to output 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" width="200">

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

JavaScript Array Sorting

Imagine that you wanted to sort an array alphabetically before you wrote the array to the browser. Well, this code has already been written and can be accessed by using the Array’s sort method.
<script type="text/javascript">
<!--
var myArray2= new Array();

myArray2[0] = "Football";
myArray2[1] = "Baseball";
myArray2[2] = "Cricket";

document.write(myArray2.sort());

//-->
</script>
Output:
 Baseball,Cricket,Football
Get Array length:
<html>
<head>
<title>Get Array length</title>

<script language="javascript" type="text/javascript">
<!--

var x = new Array(2,3,4);

alert(x.length);

//-->
</script>

</head>
<body>

</body>
</html>

0 comments:

Post a Comment

 
Top
Blogger Template