-
You are here: JS-methods > Documentation > Array

Array prototype extensions

Extends array prototype with the following methods: contains, every, exfiltrate, filter, forEach, getRange, inArray, indexOf, insertAt, map, randomize, removeAt, some, unique

This extensions doesn't depend on any other code or overwrite existing methods.

  • contains(Array elements) : Boolean
    Returns true if every element in 'elements' is in the array.

    Example:

    [1, 2, 1, 4, 5, 4].contains([1, 2, 4]);

    Result:

    true
  • every(Function fn, Object scope) : Boolean
    Tests whether all elements in the array pass the test implemented by the provided function.

    Example:

    [22, 72, 16, 99, 254].every(function(element, index, array) {
    	  return element >= 15;
    	});

    Result:

    true;

    Example:

    [12, 72, 16, 99, 254].every(function(element, index, array) {
    	  return element >= 15;
    	});

    Result:

    false;
  • exfiltrate(Array elements) : Boolean
    Returns the array without the elements in 'elements'.

    Example:

    [1, 2, 1, 4, 5, 4].contains([1, 2, 4]);

    Result:

    true
  • filter(Function fn, Object scope) : Array
    Creates a new array with all elements that pass the test implemented by the provided function.

    Natively supported in Gecko since version 1.8.

    Example:

    [12, 5, 8, 1, 44].filter(function(element, index, array) {
    	  return element >= 10;
    	});

    Result:

    [12, 44];
  • forEach(Function fn, Object scope) : void
    Executes a provided function once per array element.

    Natively supported in Gecko since version 1.8. http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach

    Example:

    var stuff = "";
    	["Java", "Script"].forEach(function(element, index, array) {
    	  stuff += element;
    	});

    Result:

    "JavaScript";
  • getRange(Number startIndex, Number endIndex) : Array
    Returns a range of items in this collection

    Example:

    [1, 2, 1, 4, 5, 4].getRange(2, 4);

    Result:

    [1, 4, 5]
  • inArray(Object subject) : Boolean
    Checks if a given subject can be found in the array.

    Example:

    [12, 5, 7, 5].inArray(7);

    Result:

    true;

    Example:

    [12, 5, 7, 5].inArray(9);

    Result:

    false;
  • indexOf(Object subject, Number offset) : Int
    Returns the first index at which a given element can be found in the array, or -1 if it is not present.

    Example:

    [12, 5, 8, 5, 44].indexOf(5);

    Result:

    1;

    Example:

    [12, 5, 8, 5, 44].indexOf(5, 2);

    Result:

    3;
  • insertAt(Number index, Object element) : Array
    Inserts an item at the specified index in the array.

    Example:

    ['dog', 'cat', 'horse'].insertAt(2, 'mouse');

    Result:

    ['dog', 'cat', 'mouse', 'horse']
  • map(Function fn, Object scope) : Array
    Creates a new array with the results of calling a provided function on every element in this array.

    Natively supported in Gecko since version 1.8. http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:map

    Example:

    ["my", "Name", "is", "HARRY"].map(function(element, index, array) {
    	  return element.toUpperCase();
    	});

    Result:

    ["MY", "NAME", "IS", "HARRY"];

    Example:

    [1, 4, 9].map(Math.sqrt);

    Result:

    [1, 2, 3];
  • randomize() : Array
    Randomize the order of the elements in the Array.

    Example:

    [2, 3, 4, 5].randomize();

    Result:

    [5, 2, 3, 4] randomized result
  • removeAt(Number index) : Array
    Remove an item from a specified index in the array.

    Example:

    ['dog', 'cat', 'mouse', 'horse'].deleteAt(2);

    Result:

    ['dog', 'cat', 'horse']
  • some(Function fn, Object scope) : Boolean
    Tests whether some element in the array passes the test implemented by the provided function.

    Natively supported in Gecko since version 1.8. http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:some

    Example:

    [101, 199, 250, 200].some(function(element, index, array) {
    	  return element >= 100;
    	});

    Result:

    true;

    Example:

    [101, 99, 250, 200].some(function(element, index, array) {
    	  return element >= 100;
    	});

    Result:

    false;
  • unique() : Array
    Returns a new array that contains all unique elements of this array.

    Example:

    [1, 2, 1, 4, 5, 4].unique();

    Result:

    [1, 2, 4, 5]