A collection of Array methods.
Calls a function for each element in the array.
myArray.each(fn[, bind]);
fn(item, index, array)
//Alerts "0 = apple", "1 = banana", and so on: ['apple', 'banana', 'lemon'].each(function(item, index){ alert(index + " = " + item); }); //The optional second argument for binding isn't used here.
Returns true if every element in the array satisfies the provided testing function. This method is provided only for browsers without native Array:every support.
var allPassed = myArray.every(fn[, bind]);
fn(item, index, array)
var areAllBigEnough = [10, 4, 25, 100].every(function(item, index){ return item > 20; }); //areAllBigEnough = false
Creates a new array with all of the elements of the array for which the provided filtering function returns true. This method is provided only for browsers without native Array:filter support.
var filteredArray = myArray.filter(fn[, bind]);
fn(item, index, array)
var biggerThanTwenty = [10, 3, 25, 100].filter(function(item, index){ return item > 20; }); //biggerThanTwenty = [25, 100]
Creates a new array with all of the elements of the array which are defined (i.e. not null or undefined).
var cleanedArray = myArray.clean();
var myArray = [null, 1, 0, true, false, "foo", undefined, ""]; myArray.clean() // returns [1, 0, true, false, "foo", ""]
Returns the index of the first element within the array equal to the specified value, or -1 if the value is not found. This method is provided only for browsers without native Array:indexOf support.
var index = myArray.indexOf(item[, from]);
['apple', 'lemon', 'banana'].indexOf('lemon'); //returns 1 ['apple', 'lemon'].indexOf('banana'); //returns -1
Creates a new array with the results of calling a provided function on every element in the array. This method is provided only for browsers without native Array:map support.
var mappedArray = myArray.map(fn[, bind]);
fn(item, index, array)
var timesTwo = [1, 2, 3].map(function(item, index){ return item * 2; }); //timesTwo = [2, 4, 6];
Returns true if at least one element in the array satisfies the provided testing function. This method is provided only for browsers without native Array:some support.
var somePassed = myArray.some(fn[, bind]);
fn(item, index, array)
var isAnyBigEnough = [10, 4, 25, 100].some(function(item, index){ return item > 20; }); //isAnyBigEnough = true
Creates an object with key-value pairs based on the array of keywords passed in and the current content of the array.
var associated = myArray.associate(obj);
var animals = ['Cow', 'Pig', 'Dog', 'Cat']; var sounds = ['Moo', 'Oink', 'Woof', 'Miao']; sounds.associate(animals); //returns {'Cow': 'Moo', 'Pig': 'Oink', 'Dog': 'Woof', 'Cat': 'Miao'}
Accepts an object of key / function pairs to assign values.
var result = Array.link(array, object);
var el = document.createElement('div'); var arr2 = [100, 'Hello', {foo: 'bar'}, el, false]; arr2.link({myNumber: Number.type, myElement: Element.type, myObject: Object.type, myString: String.type, myBoolean: $defined}); //returns {myNumber: 100, myElement: el, myObject: {foo: 'bar'}, myString: 'Hello', myBoolean: false}
Tests an array for the presence of an item.
var inArray = myArray.contains(item[, from]);
["a","b","c"].contains("a"); //returns true ["a","b","c"].contains("d"); //returns false
Extends an array with all the items of another.
myArray.extend(array);
var animals = ['Cow', 'Pig', 'Dog']; animals.extend(['Cat', 'Dog']); //animals = ['Cow', 'Pig', 'Dog', 'Cat', 'Dog'];
Returns the last item from the array.
myArray.getLast();
['Cow', 'Pig', 'Dog', 'Cat'].getLast(); //returns 'Cat'
Returns a random item from the array.
myArray.getRandom();
['Cow', 'Pig', 'Dog', 'Cat'].getRandom(); //returns one of the items
Pushes the passed element into the array if it's not already present (case and type sensitive).
myArray.include(item);
['Cow', 'Pig', 'Dog'].include('Cat'); //returns ['Cow', 'Pig', 'Dog', 'Cat'] ['Cow', 'Pig', 'Dog'].include('Dog'); //returns ['Cow', 'Pig', 'Dog']
Combines an array with all the items of another. Does not allow duplicates and is case and type sensitive.
myArray.combine(array);
var animals = ['Cow', 'Pig', 'Dog']; animals.combine(['Cat', 'Dog']); //animals = ['Cow', 'Pig', 'Dog', 'Cat'];
Removes all occurrences of an item from the array.
myArray.erase(item);
['Cow', 'Pig', 'Dog', 'Cat', 'Dog'].erase('Dog') //returns ['Cow', 'Pig', 'Cat'] ['Cow', 'Pig', 'Dog'].erase('Cat') //returns ['Cow', 'Pig', 'Dog']
Empties an array.
myArray.empty();
var myArray = ['old', 'data']; myArray.empty(); //myArray is now []
Flattens a multidimensional array into a single array.
myArray.flatten();
var myArray = [1,2,3,[4,5, [6,7]], [[[8]]]]; var newArray = myArray.flatten(); //newArray is [1,2,3,4,5,6,7,8]
Converts an hexidecimal color value to RGB. Input array must be the following hexidecimal color format. ['FF','FF','FF']
myArray.hexToRgb([array]);
['11','22','33'].hexToRgb(); //returns "rgb(17,34,51)" ['11','22','33'].hexToRgb(true); //returns [17, 34, 51]
Converts an RGB color value to hexidecimal. Input array must be in one of the following RGB color formats. [255,255,255], or [255,255,255,1]
myArray.rgbToHex([array]);
[17,34,51].rgbToHex(); //returns "#112233" [17,34,51].rgbToHex(true); //returns ['11','22','33'] [17,34,51,0].rgbToHex(); //returns "transparent"
Creates a copy of an Array. Useful for applying the Array prototypes to iterable objects such as a DOM Node collection or the arguments object.
var copiedArray = $A(iterable);
function myFunction(){ $A(arguments).each(function(argument, index){ alert(argument); }); }; myFunction("One", "Two", "Three"); //Alerts "One", then "Two", then "Three".
var anArray = [0, 1, 2, 3, 4]; var copiedArray = $A(anArray); //Returns [0, 1, 2, 3, 4].
This documentation is released under a Attribution-NonCommercial-ShareAlike 3.0 License.