Arrays and tables in Javascript
Javascript does not have an explicit array data type. However, you can get use the predefined Array object and its methods to work with arrays in your application. The array object has methods for manipulating arrays in various ways, such as joining, reversing and sorting them. It has a property for determining the array length and other properties for use with regular expressions.
An array is a ordered set of values that you refer to with a name and an index. For example, you could have an array called emp that contains employees' names indexed by their employee number. So emp[1] would be employee number one, emp[2] employee number two, and so one.
To create an Array object:
arrayObjectName = new Array(element0,element1,...,elementn) arrayObjectName = new Array(arrayLength)arrayObjectName is either the name of a new object or a property of an existing object. When using Array properties and methods, arrayObjectName is either the name of an existing Array object or a property of an existing object.
element0, element1, .. elementn is a list of values for the array's elements. When this form is specified values as its elements, and the array's length property is set to the number of arguments.
arrayLength is the initial length of the array. The following code creates an array of 5 elements.
billingMethod = new Array(5)Array literals are also Array objects; for example, the following literal is an Array object:
coffees = ["French Roast", "Colombian", "Kona"]For more information on array literals, go here
Populating an Array
You can populate an array by assigning values to its elements.
emp[1] = "Casey Jones" emp[2] = "Phil Lesh" emp[3] = "August West"You can also populate an array when you create it:
myArray = new Array("Hello", myVar, 3.14159)Referring to Array elements
You refer to an array's elements by using the element's ordinal number. For example, suppose you define the following array.
myArray = new Array("wind","rain","fire")You then refer to the first element of the array as
myArray[0]
and the second element of the array asmyArray[1]
The index of the elements begins with zero (0), but the length of array (for example, myArray.length) reflects the number of elements in the array.
Array Methods
- concat joins two arrays and returns a new array.
- join Joins all element of a array into a string.
- pop removes the last element from an array and returns that element.
- push adds one or more elements to the end of an array and returns the last element added.
- reverse transposes the element of an array: the first array element becomes the last and the last becomes the first.
- shift removes the first elementfrom an array and returns that element.
- slice extracts a section of an array and returns a new array.
- splice adds and/or removes elements from an array.
- sort sorts the elements of an array.
- unshift adds one or more elements to the front of an array and returns the new length of the array.
Two-Dimensional Arrays
The following code creates a new array.
a = new array(4) for (i=0; t<4; i++) { a[i] = new array(4) for (j=0; j<4;j++) { a[i][j] = "["+i+","+j+"]" } }The following code displays the array.
for (i=0; t<4; i++) { str = "Row "+i+":" for (j=0; j<4;j++) { str += a[i][j] } document.write(str,"<br>") }This example displays the following results:
Row 0:[0,0][0,1][0,2][0,3] Row 1:[1,0][1,1][1,2][1,3] Row 2:[2,0][2,1][2,2][2,3] Row 3:[3,0][3,1][3,2][3,3]Arrays and Regular Expressions
When an array is the result of a match between a regular expression and a string, the array returns properties and elements that provide information about the match. An array is the return value of regexp.exec, sting.match and string.replace.