Ajax JavaScript CSS HTML 5 Gecko XUL Forum

Array object in JavaScript, reference and interactive test

Before the online test of attributes and methods of the Array object, a description of each.

The Array object was first implemented in JavaScript version 1.1 and has been augmented in version 1.3, that is compatible with the ECMA-262 standard as is the version 1.5 which is described here.

The constructor has two sort of parameters

The constructor supports for argument, either the size of the array, or a list of contained items.

The syntax is:

var x = new Array(number);
ou
var x = new Array(item0, item1, etc...);

Example:

var x = new Array("one", "two", 254);

A variable may be an argument as any object:

var y = "demo";
var x = new Array(y);
document.write(x[0]);

You can also create a array from a literal:

var a = [ "one", "two", 254 ];

The variable declared in a such way will get the attributes and methods of the Array object.

Items are accessed by an index

Arrays, whether created from a literal or a constructor, are objects. Elements can be accessed by the index as explained in the chapter Arrays and JavaScript.

Example:

var x = new Array("one", "two", 254);
document.write(x[2])   // must return 254

Size and regular expression attributes

Array has the following attributes:

int length

int index

For a array that is the result of the test of a regular expression, is the index of the string found.

String input

For a array that is the result of a test of a regular expression is the initial string on which the test is applied.

Trying interactively methods to access items or transform the array

As with any object, you can invoke methods associated with the name of an instance of Array.

var x = new Array();
x.push("a");

From a set of strings that you give to create the array, you will test methods of this object.

Enter arguments for the constructor of Array:

The so created object will be used by attributes and methods corresponding to buttons below.

position

with this array

separator

start end

start remove (Adds items of the array after concat above)

Description of all the methods
void push (x)
Add an element to the end of the array.
var y = x.pop()
Returns the last element in the array, and removes it.
String s = x.join ([separator])
Creates a string of characters from the elements of the array. The array is unchanged.
[separator] is an optional separator in parameter.
Array z = x.concat (y)
Concatenates the contents of array x with array y and returns a new array z.
The original x array is not modified.
void sort(function)
Orders the elements by applying the function given in argument.
Array x = slice(int beginning [, int end])
Returns a subset of the array, which remains unchanged.
void splice(pos, number, item0, item1, ...)
Starting from the position pos, deletes the given number of elements in the second argument, and insert the elements of the list that follows. If 0 have to be removed, at least one element must be added to.
With splice, the content of the array itself is changed.
var x =shift()
Returns the first element of the array and removes it.
void unshift(x)
Inserts an element at the beginning of the array by shifting the contents by one position.
String toString()
Provides a representation in string of characters of the array. Like join() but always with commas like separators.
void reverse()
Reverses the order of elements.

See also

© 2008-2011 Xul.fr