Built-In Namespace Array
| Method Attributes | Method Name and Description |
|---|---|
|
collect(property)
Special-case implementation of Array#map to collect the same property from every element of an Array
|
|
|
every(callback, context)
Tests whether all elements in the array pass the test implemented by the provided function
|
|
|
filter(callback, context)
Creates a new array with all elements that pass the test implemented by the provided function.
|
|
|
forEach(callback, context)
Executes a provided function once per array element.
|
|
| <static> |
Array.forEach(array, callback, context)
Executes a provided function once per array-like element.
|
|
inArray(value)
Convenience method to check if a value is in an array
|
|
|
indexOf(searchElement, from)
Returns the first index at which a given element can be found in the array, or -1 if it is not present
|
|
|
lastIndexOf(searchElement, from)
Returns the last index at which a given element can be found in the array, or -1 if it is not present.
|
|
|
map(callback, context)
Creates a new array with the results of calling a provided function on every element in this array
|
|
|
reduce(callback, initial)
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
|
|
|
reduceRight(callback, initial)
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
|
|
|
some(callback, context)
Tests whether some element in the array passes the test implemented by the provided function
|
|
|
unique()
Special-case implementation of Array#filter to remove duplicate entries from an Array
|
Method Detail
{Array}
collect(property)
Special-case implementation of Array#map to collect the same property from every element of an Array
Defined in: array.js.
Defined in: array.js.
- Parameters:
- {String} property
- the name of the property to collect from each Array element
- Returns:
- {Array} a new Array with the specified property as the element values
- Requires:
- Array#map
{Boolean}
every(callback, context)
Tests whether all elements in the array pass the test implemented by the provided function
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough); //false
passed = [12, 54, 18, 130, 44].every(isBigEnough); //true
- Parameters:
- {Function} callback
- function to test for each element
- {Object} context Optional
- to use as this when executing callback
- Returns:
- {Boolean} true if all elements pass the test implented by callback, false if not
{Array}
filter(callback, context)
Creates a new array with all elements that pass the test implemented by the provided function.
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
function isBigEnough(element, index, array) {
return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
- Parameters:
- {Function} callback
- function to test each element of the array
- {Object} context Optional
- to use as this when executing callback
- Returns:
- {Array} a new array with all elements that pass the test implemented by the provided function
forEach(callback, context)
['moo', 'cow', 'woof', 'dog'].forEach(alert);
- Parameters:
- {Function} callback
- function to execute for each element
- {Object} context Optional
- to use as this when executing callback
<static>
Array.forEach(array, callback, context)
Executes a provided function once per array-like element.
Defined in: array.js.
Author: Dean Edwards.
Defined in: array.js.
Author: Dean Edwards.
Array.forEach(document.getElementsByTagName('div'),alert);
- Parameters:
- {Array-like} array
- Array-like object to iterate over
- {Function} callback
- function to execute for each element
- {Object} context Optional
- to use as this when executing callback
{Boolean}
inArray(value)
Convenience method to check if a value is in an array
Defined in: array.js.
Defined in: array.js.
var test = [0, '1', 2].inArray(1); //false
test = [0, '1', 2].inArray('1'); //true
- Parameters:
- value
- value to check for in array
- Returns:
- {Boolean} true if value is found, false if not
- Requires:
- Array#indexOf
{Number}
indexOf(searchElement, from)
Returns the first index at which a given element can be found in the array, or -1 if it is not present
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
- Parameters:
- searchElement
- Element to locate in the array
- {Number} from Optional, Default: 0
- the index at which to begin the search. If negative, it is taken as the offset from the end of the array
- Returns:
- {Number} the index the element was found at, or -1 if not found
{Number}
lastIndexOf(searchElement, from)
Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at from
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
- Parameters:
- searchElement
- value to find the last index of
- {Number} from Optional, Default: index of the last entry
- the index to start searching from
- Returns:
- {Number} the index the value was found at, or -1 if not found
{Array}
map(callback, context)
Creates a new array with the results of calling a provided function on every element in this array
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
function makePseudoPlural(single) {
return single.replace(/o/g, "e");
}
var singles = ["foot", "goose", "moose"];
var plurals = singles.map(makePseudoPlural); //["feet", "geese", "meese"]
- Parameters:
- {Function} callback
- function that produces an element of the new Array from an element of the current one
- {Object} context Optional
- to use as this when executing callback
- Returns:
- {Array} new array with the results of calling a provided function on every element in this array
reduce(callback, initial)
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; }); //6
var flattened = [[0,1], [2,3], [4,5]].reduce(function(a,b) {
return a.concat(b);
}, []); //[0, 1, 2, 3, 4, 5]
- Parameters:
- {Function} callback
- function to execute on each value in the array
- initial Optional
- object to use as the first argument to the first call of the callback
- Returns:
- the value calculated by executing callback on each element of the array
- See:
- http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
- Array.prototype#reduceRight
reduceRight(callback, initial)
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
var total = [0, 1, 2, 3].reduceRight(function(a, b) { return a + b; }); //6
var flattened = [[0,1], [2,3], [4,5]].reduceRight(function(a,b) {
return a.concat(b);
}, []); //flattened is [4, 5, 2, 3, 0, 1]
- Parameters:
- {Function} callback
- function to execute on each value in the array
- initial Optional
- object to use as the first argument to the first call of the callback
- Returns:
- the value calculated by executing callback on each element of the array
- See:
- http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
- Array#reduce
{Boolean}
some(callback, context)
Tests whether some element in the array passes the test implemented by the provided function
Defined in: array.js.
Author: Mozilla.
Defined in: array.js.
Author: Mozilla.
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].every(isBigEnough); //false
passed = [12, 5, 8, 1, 4].every(isBigEnough); //true
- Parameters:
- {Function} callback
- fucntion to test for each element
- {Object} context Optional
- to use as this when executing callback
- Returns:
- {Boolean} true if some element passes the test implented by callback, false if not
{Array}
unique()
Special-case implementation of Array#filter to remove duplicate entries from an Array
Defined in: array.js.
Defined in: array.js.
- Returns:
- {Array} a new Array with the duplicate elements removed
- Requires:
- Array#filter