Array object
The Array object lets you store a group of data in a variable.
The elements in a JavaScript array can have different data types, and they can be accessed by indexing.
Elements of an array can also be arrays. In that case, the array is a multi-dimensional array. The elements within a multi-dimensional array are accessed using more than one index: one for each dimension. Two dimensional arrays can be visualized as a multicolumn table, and a multi-dimensional (one with more than two dimensions) as a tree.
JavaScript allows using associative arrays, too. In this type of arrays, each stored value is associated with a unique key, which can be used as an index. The unique keys are string types. In JavaScript, every object can behave like an associative array.
In the 'Array operations' section below, you can find examples that illustrate the use of associative arrays.
Elements of an array can also be arrays. In that case, the array is a multi-dimensional array. The elements within a multi-dimensional array are accessed using more than one index: one for each dimension. Two dimensional arrays can be visualized as a multicolumn table, and a multi-dimensional (one with more than two dimensions) as a tree.
JavaScript allows using associative arrays, too. In this type of arrays, each stored value is associated with a unique key, which can be used as an index. The unique keys are string types. In JavaScript, every object can behave like an associative array.
How to use associative arrays?
You can set a member of an arbitrary object in two ways:
- object.member = value
- object["member"] = value
- value = object.member
- value = object["member"]
- object.member (param1, param2, ...)
- object["member"] (param1, param2, ...)
Syntax:
Creating an empty Array object:
var arr = new Array ();
Creating an Array object with initial length:
var arr = new Array ([length]);
- If length is not specified, creates an empty array.
- The value of the length parameter must be a non-negative integer.
- If the type of the length parameter is not a number, then creates a one-element array containing the specified value. See the 'Creates a new Array object with initial elements' section below.
- If the type of the length parameter is a number, then it must be a non-negative integer, else it causes error.
- The elements are undefined in an array that is created with initial length.
var arr = new Array (element1, element2, ..., elementN);
The newly created array contains the specified elements.
- If no elements are specified, creates an empty array.
- If only one element is specified and it is a number, then it defines the length of the array! See the 'Creates a new Array object with initial length' section above.
var arr = [element1, element2, ..., elementN];
The newly created array contains the specified elements.
If no elements are specified, creates an empty array.
Array operations
Creating a simple array:
Creating a multidimensional array (array of arrays):
The multiArr variable is an array that has two elements, the first element is an array (A,B,C), and the second element is another array with elements (1,2,3).
Multidimensional arrays are useful for example if you want to store the full contents of a table element in an array.
Iterating through a simple array:
Adding elements to an array:
Joining two array objects into a new one:
Deleting elements from a simple array:
Creating an associative array:
Iterating through an associative array:
Deleting elements from an associative array:
|
Members:
The Array object inherits from the Array.prototype and Function.prototype objects.
The following lists only contain the members of the Array and Array.prototype objects.
(*) - The property is inherited from the Array.prototype.
(*) - The method is inherited from the Array.prototype.
Properties:
Property | Support | Description | |||||
---|---|---|---|---|---|---|---|
index* | Works only for regular expression arrays returned by the exec method. Returns an integer value that indicates the zero-based starting position of the match in the original string. | ||||||
input* | Returns the original string, for regular expression arrays returned by the exec method. | ||||||
length* | Returns the number of elements in the current array. | ||||||
prototype | Returns a reference to the Array.prototype object. The Array.prototype object allows adding properties and methods to the Array object that can be used with instances of the Array object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Array object, only Array.prototype is allowed. |
(*) - The property is inherited from the Array.prototype.
Methods:
Method | Support | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
[index] | Returns the element at the specified position. | |||||||||||
concat ([array1 [,arrayN]...])* | Joins the current array with the specified arrays and returns the combined array. | |||||||||||
every (function [, thisObject])* |
|
Checks whether the given function returns true for all elements in the array, and if so, it returns true, else it returns false. | ||||||||||
filter (function [, thisObject])* |
|
Executes the given function for each element in the array and retrieves an array filled with elements for which the given function returns true. | ||||||||||
forEach (function [, thisObject])* |
|
Executes the given function for each element in the array. | ||||||||||
indexOf (elemToSearch [, fromIndex])* |
|
Tries to find the given elemToSearch element within the array and returns the index of the first match or -1. | ||||||||||
join ([separator])* | Returns a string that contains the value of the elements in the array separated by the given separator. | |||||||||||
lastIndexOf (elemToSearch [, startIndex])* |
|
Tries to find the given elemToSearch element backwards within the array and returns the index of the first match or -1. | ||||||||||
map (function [, thisObject])* |
|
Executes the given function for each element in the array and returns a new array filled with the return values. | ||||||||||
pop ( )* | Removes the last element from the array and returns the removed element. | |||||||||||
push ([newElement1 [,newElementN]...])* | Adds one or more elements to the end of the array and returns the new length of the array. | |||||||||||
reverse ( )* | Returns a new array with the elements of the original array but in reverse order. | |||||||||||
shift ( )* | Removes the first element from the array and returns the removed element. | |||||||||||
slice (start, [end])* | Returns an array filled with the elements of the current array from the start position to end. | |||||||||||
some (function [, thisObject])* |
|
Checks whether the given function returns true for at least one element in the array, and if so, it returns true, else it returns false. | ||||||||||
sort ([compareFunction])* | If the compareFunction is not defined, sorts the elements of the array in lexicographical order, else the order of the elements is based on the compareFunction method. | |||||||||||
splice (start, howmany [, newElement1 [,newElementN]...])* | Inserts and removes elements from the array. | |||||||||||
toSource ( )* | Returns a string representing the source code of the current array. | |||||||||||
toString ( )* | Returns a string that contains the value of the elements in the array separated by commas. If you want to concatenate the values with a separator other than comma, use the join method. | |||||||||||
unshift (newElement1 [, newElement2 ... [, newElementN]])* | Inserts one or more elements at the beginning of the array. |
(*) - The method is inherited from the Array.prototype.
Properties
index
Works only for regular expression arrays returned by the exec method.
Returns an integer value that indicates the zero-based start position of the match in the original string.
Output:
Displays the starting and end positions of the words in the str (string) variable.
0-3 The 4-9 apple 10-12 is 13-14 a 15-19 tree 20-23 and 24-27 its 28-37 pomaceous 38-43 fruit |
|||||||||||||
input
Returns the original string, for regular expression arrays returned by the exec method.
|
|||||||||||||
length
Returns the number of elements in the current array.
|
|||||||||||||
prototype
Allows adding properties and methods to the Array object, which can be used like any predefined property or method listed here.
|
Methods
[index]
Returns the element at the specified position.
Return value: Returns the element at the specified position. If the position is not valid, returns undefined.
|
||||||||||||||||||||||||||
concat ([array1 [,arrayN]...])
Joins the current array with the specified arrays and returns the combined array.
The current array is not changed by the concat method.
Return value: Returns the newly created array.
|
||||||||||||||||||||||||||
every (function [, thisObject])
Checks whether the given function returns true for all elements in the array, and if so, it returns true, else it returns false.
Return value: Returns the calculated boolean value.
|
||||||||||||||||||||||||||
filter (function [, thisObject])
Executes the given function for each element in the array and retrieves an array filled with elements for which the given function returns true.
Return value: Returns the newly created array.
|
||||||||||||||||||||||||||
forEach (function [, thisObject])
Executes the given function for each element in the array.
Return value: This function does not return a value.
|
||||||||||||||||||||||||||
indexOf (elemToSearch [, fromIndex])
Tries to find the given elemToSearch element within the array and returns the index of the first match or -1.
An element of the array match the elemToSearch element if their values and types are equal (strict equal operator ===).
Return value: Returns the index of the first match or -1 if no match is found.
|
||||||||||||||||||||||||||
join ([separator])
Returns a string that contains the value of the elements in the array separated by the given separator.
The join method converts the values to string first then concatenates them using the separator.
Return value: Returns the string created from the elements of the array.
|
||||||||||||||||||||||||||
lastIndexOf (elemToSearch [, fromIndex])
Tries to find the given elemToSearch element backwards within the array and returns the index of the first match or -1.
An element of the array matches the elemToSearch element if their values and types are equal (strict equal operator ===).
Return value: Returns the index of the first match or -1 if no match is found.
|
||||||||||||||||||||||||||
map (function [, thisObject])
Executes the given function for each element in the array and returns a new array filled with the return values.
Return value: Returns the newly created array.
|
||||||||||||||||||||||||||
pop ()
Removes the last element from the array and returns the removed element.
Return value: Returns the removed element or undefined if the array is empty.
|
||||||||||||||||||||||||||
push ([newElement1 [,newElementN]...])
Adds one or more elements to the end of the array and returns the new length of the array.
Return value: Returns the new length of the array.
|
||||||||||||||||||||||||||
reverse ()
Returns a new array filled with the elements of the original array but in reverse order.
Return value: Returns the newly created array.
|
||||||||||||||||||||||||||
shift ()
Removes the first element from the array and returns the removed element.
Return value: Returns the removed element or undefined if the array is empty.
|
||||||||||||||||||||||||||
slice (start, [end])
Returns an array, filled with the elements of the current array from the position start to end.
Return value: Returns the newly created array.
|
||||||||||||||||||||||||||
some (function [, thisObject])
Checks whether the given function returns true for at least one element in the array, and if so, it returns true, else it returns false.
Return value: Returns the calculated boolean value.
|
||||||||||||||||||||||||||
sort ([compareFunction])
If the compareFunction is not defined, sorts the elements of the array in lexicographical order, else the order of the elements is based on the compareFunction method.
The compareFunction defines an ordering for any two elements in the array.
It gets two parameters (a and b) from the sort method, where a and b are arbitrary elements from the array.
If a is less than b by the ordering criterion, the compareFunction must return a value less than 0. If a is greater than b by the ordering criterion, the compareFunction must return a value greater than 0. If a and b are equal, it must return 0. The sort method uses this returned value for sorting the elements.
Return value: Returns the sorted array.
1. Example:
This example uses the sort method without a compareFunction parameter.
2. Example:
This example uses the sort method with a compareFunction parameter.
|
||||||||||||||||||||||||||
splice (start, howmany [, newElement1 [,newElementN]...])
Inserts and removes elements from the array.
Return value: Returns an array filled with the removed elements.
|
||||||||||||||||||||||||||
toSource ()
Returns a string representing the source code of the current array.
Return value: Returns a string representing the source code.
|
||||||||||||||||||||||||||
toString ()
Returns a string that contains the value of the elements in the array separated by commas.
When an array needs to be converted to a string, the JavaScript interpreter automatically calls its toString method. If you want to concatenate the values with a separator other than comma, use the join method.
Return value: Returns a string representing the value.
|
||||||||||||||||||||||||||
unshift (newElement1 [, newElement2 ... [, newElementN]])
Inserts one or more elements at the beginning of the array.
Return value: Returns the new length of the array.
|
External links:
User Contributed Comments