The Arrays Object
The Arrays Object
The Array object let's you store multiple values in a single variable.
Syntax:
Creating a Array object:
var ArrayVar = new Array( length);
The Array parameter is a list of strings or integers. When you specify
a single numeric parameter with the Array constructor, you
specify the initial length of the array. The maximum length
allowed for an array is 4,294,967,295.
The Array constructor syntax has three different forms. If you call
the constructor with two or more arguments, the arguments
initialize the array elements. If you only supply one argument to
the Array constructor, the argument initializes the length of the
new array; the new array’s elements are not initialized. Finally, if
you call the constructor without arguments, the new array’s
length is set to zero, and its elements are not initialized.
Here are examples:
var myArray4 = new Array(1,3,5,7,9) // an array with 5 elements
var myArray5 = new Array(100) // an empty array of length 100
var myArray6 = new Array() // an empty array of length 0
You can create an array using either an array initializer (array literal) or
the Array constructor.
The array initializer (array literal) syntax is simple: a comma-separated list of
values in square brackets. Here are some examples:
var myArray1 = [1,3,5,7,9] // an array with 5 elements
var myArray2 = [5] // an array with 1 element
An array can store anything you can assign to a variable: booleans, numbers,
strings, functions, objects, other Arrays, even regular expressions…
var myArray = [ 3, 'hello!', function() {return 5}, {'color':'blue', 'budget':25},
/[ell]/i ];
document.writeln('0>'+myArray[0]+'<BR>'); // Will output: 0>3
document.writeln('1>'+myArray[1]+'<BR>'); // Will output: 1>hello!
document.writeln('2>'+myArray[2]()+'<BR>'); // Will output: 2>5
document.writeln('3>'+myArray[3].color+'<BR>'); // Will output: 3>blue
document.writeln('3>'+myArray[3].budget+'<BR>'); // Will output: 3>25
document.writeln('4>'+myArray[4].test(myArray[1])+'<BR>'); // Will output:
4>true
JavaScript does not have a special syntax for creating
multidimensional arrays. A common workaround is to create
an array of arrays in nested loops.
The following code example illustrates the array-of-arrays
technique. First, this code creates an array f. Then, in the
outer for loop, each element of f is itself initialized as new
Array(); thus f becomes an array of arrays. In the inner for loop,
all elements f[i][ j] in each newly created "inner" array are set to
zero.
var iMax = 20;
var jMax = 10;
var f = new Array();
for (i=0;i<iMax;i++)
{ f[i]=new Array();
for ( j=0;j<jMax;j++)
{
f[i][ j]=0;
}
}
Array Properties
push() Adds one or more elements to the end of an array and returns the new length array.push(element1, ...,
of the array. elementN);
reverse() Reverses the order of the elements of an array -- the first becomes the last, array.reverse();
and the last becomes the first.
shift() Removes the first element from an array and returns that element. array.shift();
slice() Extracts a section of an array and returns a new array. begin : Zero-based array.slice( begin [,end] );
index at which to begin extraction. As a negative index, start indicates an
offset from the end of the sequence.
end : Zero-based index at which to end extraction.
some() Returns true if at least one element in this array satisfies the provided testing array.some(callback[,
function. thisObject]);
sort() Sorts the elements of an array. compareFunction : Specifies a function that array.sort( compareFunctio
defines the sort order. If omitted, the array is sorted lexicographically. n );
unshift() Adds one or more elements to the front of an array and returns the new length array.unshift( element1, ...,
of the array. elementN );