the compact javascript framework
in partnership with mediatemple
Contains Array prototypes, $A, $each
MIT-style license.
Array.js | Contains Array prototypes, $A, $each |
Array | A collection of The Array Object prototype methods. |
Properties | |
forEach | Iterates through an array; This method is only available for browsers without native forEach support. |
filter | This method is provided only for browsers without native filter support. |
map | This method is provided only for browsers without native map support. |
every | This method is provided only for browsers without native every support. |
some | This method is provided only for browsers without native some support. |
indexOf | This method is provided only for browsers without native indexOf support. |
each | Same as Array.forEach. |
copy | returns a copy of the array. |
remove | Removes all occurrences of an item from the array. |
contains | Tests an array for the presence of an item. |
associate | Creates an object with key-value pairs based on the array of keywords passed in and the current content of the array. |
extend | Extends an array with another one. |
merge | merges an array in another array, without duplicates. |
include | includes the passed in element in the array, only if its not already present. |
getRandom | returns a random item in the Array |
getLast | returns the last item in the Array |
Utility Functions | |
Functions | |
$A() | Same as Array.copy, but as function. |
$each | Use to iterate through iterables that are not regular arrays, such as builtin getElementsByTagName calls, arguments of a function, or an object. |
A collection of The Array Object prototype methods.
Properties | |
forEach | Iterates through an array; This method is only available for browsers without native forEach support. |
filter | This method is provided only for browsers without native filter support. |
map | This method is provided only for browsers without native map support. |
every | This method is provided only for browsers without native every support. |
some | This method is provided only for browsers without native some support. |
indexOf | This method is provided only for browsers without native indexOf support. |
each | Same as Array.forEach. |
copy | returns a copy of the array. |
remove | Removes all occurrences of an item from the array. |
contains | Tests an array for the presence of an item. |
associate | Creates an object with key-value pairs based on the array of keywords passed in and the current content of the array. |
extend | Extends an array with another one. |
merge | merges an array in another array, without duplicates. |
include | includes the passed in element in the array, only if its not already present. |
getRandom | returns a random item in the Array |
getLast | returns the last item in the Array |
Iterates through an array; This method is only available for browsers without native forEach support. For more info see http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach
forEach executes the provided function (callback) once for each element present in the array. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
fn | function to execute with each item in the array; passed the item and the index of that item in the array |
bind | the object to bind “this” to (see <Function.bind>) |
['apple','banana','lemon'].each(function(item, index){ alert(index + " = " + item); //alerts "0 = apple" etc. }, bindObj); //optional second arg for binding, not used here
This method is provided only for browsers without native filter support. For more info see http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Objects:Array:filter
filter calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a true value. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callback test are simply skipped, and are not included in the new array.
fn | function to execute with each item in the array; passed the item and the index of that item in the array |
bind | the object to bind “this” to (see <Function.bind>) |
var biggerThanTwenty = [10,3,25,100].filter(function(item, index){ return item > 20; }); //biggerThanTwenty = [25,100]
This method is provided only for browsers without native map support. For more info see http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Global_Objects:Array:map
map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
fn | function to execute with each item in the array; passed the item and the index of that item in the array |
bind | the object to bind “this” to (see <Function.bind>) |
var timesTwo = [1,2,3].map(function(item, index){ return item*2; }); //timesTwo = [2,4,6];
This method is provided only for browsers without native every support. For more info see http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Global_Objects:Array:every
every executes the provided callback function once for each element present in the array until it finds one where callback returns a false value. If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
fn | function to execute with each item in the array; passed the item and the index of that item in the array |
bind | the object to bind “this” to (see <Function.bind>) |
var areAllBigEnough = [10,4,25,100].every(function(item, index){ return item > 20; }); //areAllBigEnough = false
This method is provided only for browsers without native some support. For more info see http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Global_Objects:Array:some
some executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some immediately returns true. Otherwise, some returns false. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
fn | function to execute with each item in the array; passed the item and the index of that item in the array |
bind | the object to bind “this” to (see <Function.bind>) |
var isAnyBigEnough = [10,4,25,100].some(function(item, index){ return item > 20; }); //isAnyBigEnough = true
This method is provided only for browsers without native indexOf support. For more info see http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf
indexOf compares a search element to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
item | any type of object; element to locate in the array |
from | integer; optional; the index of the array at which to begin the search (defaults to 0) |
['apple','lemon','banana'].indexOf('lemon'); //returns 1 ['apple','lemon'].indexOf('banana'); //returns -1
Same as Array.forEach.
fn | function to execute with each item in the array; passed the item and the index of that item in the array |
bind | optional, the object that the “this” of the function will refer to. |
var Animals = ['Cat', 'Dog', 'Coala']; Animals.each(function(animal){ document.write(animal) });
returns a copy of the array.
a new array which is a copy of the current one.
start | integer; optional; the index where to start the copy, default is 0. If negative, it is taken as the offset from the end of the array. |
length | integer; optional; the number of elements to copy. By default, copies all elements from start to the end of the array. |
var letters = ["a","b","c"]; var copy = letters.copy(); // ["a","b","c"] (new instance)
Removes all occurrences of an item from the array.
item | the item to remove |
the Array with all occurrences of the item removed.
["1","2","3","2"].remove("2") // ["1","3"];
Tests an array for the presence of an item.
item | the item to search for in the array. |
from | integer; optional; the index at which to begin the search, default is 0. If negative, it is taken as the offset from the end of the array. |
true | the item was found |
false | it wasn’t |
["a","b","c"].contains("a"); // true ["a","b","c"].contains("d"); // false
Creates an object with key-value pairs based on the array of keywords passed in and the current content of the array.
keys | the array of keywords. |
var Animals = ['Cat', 'Dog', 'Coala', 'Lizard']; var Speech = ['Miao', 'Bau', 'Fruuu', 'Mute']; var Speeches = Animals.associate(Speech); //Speeches['Miao'] is now Cat. //Speeches['Bau'] is now Dog. //...
Extends an array with another one.
array | the array to extend ours with |
var Animals = ['Cat', 'Dog', 'Coala']; Animals.extend(['Lizard']); //Animals is now: ['Cat', 'Dog', 'Coala', 'Lizard'];
merges an array in another array, without duplicates. (case- and type-sensitive)
array | the array to merge from. |
['Cat','Dog'].merge(['Dog','Coala']); //returns ['Cat','Dog','Coala']
includes the passed in element in the array, only if its not already present. (case- and type-sensitive)
item | item to add to the array (if not present) |
['Cat','Dog'].include('Dog'); //returns ['Cat','Dog'] ['Cat','Dog'].include('Coala'); //returns ['Cat','Dog','Coala']
Functions | |
$A() | Same as Array.copy, but as function. |
$each | Use to iterate through iterables that are not regular arrays, such as builtin getElementsByTagName calls, arguments of a function, or an object. |
Same as Array.copy, but as function. Useful to apply Array prototypes to iterable objects, as a collection of DOM elements or the arguments object.
function myFunction(){ $A(arguments).each(argument, function(){ alert(argument); }); }; //the above will alert all the arguments passed to the function myFunction.
Use to iterate through iterables that are not regular arrays, such as builtin getElementsByTagName calls, arguments of a function, or an object.
iterable | an iterable element or an objct. |
function | function to apply to the iterable. |
bind | optional, the ‘this’ of the function will refer to this object. |
The function argument will be passed the following arguments.
item | the current item in the iterator being procesed |
index | integer; the index of the item, or key in case of an object. |
$each(['Sun','Mon','Tue'], function(day, index){ alert('name:' + day + ', index: ' + index); }); //alerts "name: Sun, index: 0", "name: Mon, index: 1", etc. //over an object $each({first: "Sunday", second: "Monday", third: "Tuesday"}, function(value, key){ alert("the " + key + " day of the week is " + value); }); //alerts "the first day of the week is Sunday", //"the second day of the week is Monday", etc.
Same as Array.copy, but as function.
Use to iterate through iterables that are not regular arrays, such as builtin getElementsByTagName calls, arguments of a function, or an object.
Documentation by Aaron Newton & Mootools Developers, generated by NaturalDocs and GeSHi