Ext.data.JsonP.Array({"alternateClassNames":[],"aliases":{},"enum":null,"parentMixins":[],"tagname":"class","subclasses":[],"extends":null,"uses":[],"html":"

Files

In JavaScript, the Array property of the global object is a constructor for\narray instances.

\n\n

An array is a JavaScript object. Note that you shouldn't use it as an\nassociative array, use Object instead.

\n\n

Creating an Array

\n\n

The following example creates an array, msgArray, with a length of 0, then assigns values to\nmsgArray[0] and msgArray[99], changing the length of the array to 100.

\n\n
var msgArray = new Array();\nmsgArray[0] = \"Hello\";\nmsgArray[99] = \"world\";\n\nif (msgArray.length == 100)\nprint(\"The length is 100.\");\n
\n\n

Creating a Two-dimensional Array

\n\n

The following creates chess board as a two dimensional array of strings. The first move is made by\ncopying the 'P' in 6,4 to 4,4. The position 4,4 is left blank.

\n\n
var board =\n[ ['R','N','B','Q','K','B','N','R'],\n['P','P','P','P','P','P','P','P'],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n['p','p','p','p','p','p','p','p'],\n['r','n','b','q','k','b','n','r']];\nprint(board.join('\\n') + '\\n\\n');\n\n// Move King's Pawn forward 2\nboard[4][4] = board[6][4];\nboard[6][4] = ' ';\nprint(board.join('\\n'));\n
\n\n

Here is the output:

\n\n
R,N,B,Q,K,B,N,R\nP,P,P,P,P,P,P,P\n , , , , , , ,\n , , , , , , ,\n , , , , , , ,\n , , , , , , ,\np,p,p,p,p,p,p,p\nr,n,b,q,k,b,n,r\n\nR,N,B,Q,K,B,N,R\nP,P,P,P,P,P,P,P\n , , , , , , ,\n , , , , , , ,\n , , , ,p, , ,\n , , , , , , ,\np,p,p,p, ,p,p,p\nr,n,b,q,k,b,n,r\n
\n\n

Accessing array elements

\n\n

Array elements are nothing less than object properties, so they are accessed as such.

\n\n
var myArray = new Array(\"Wind\", \"Rain\", \"Fire\");\nmyArray[0]; // \"Wind\"\nmyArray[1]; // \"Rain\"\n// etc.\nmyArray.length; // 3\n\n// Even if indices are properties, the following notation throws a syntax error\nmyArray.2;\n\n// It should be noted that in JavaScript, object property names are strings. Consequently,\nmyArray[0] === myArray[\"0\"];\nmyArray[1] === myArray[\"1\"];\n// etc.\n\n// However, this should be considered carefully\nmyArray[02]; // \"Fire\". The number 02 is converted as the \"2\" string\nmyArray[\"02\"]; // undefined. There is no property named \"02\"\n
\n\n

Relationship between length and numerical properties

\n\n

An array's length property and numerical properties are connected. Here is some\ncode explaining how this relationship works.

\n\n
var a = [];\n\na[0] = 'a';\nconsole.log(a[0]); // 'a'\nconsole.log(a.length); // 1\n\na[1] = 32;\nconsole.log(a[1]); // 32\nconsole.log(a.length); // 2\n\na[13] = 12345;\nconsole.log(a[13]); // 12345\nconsole.log(a.length); // 14\n\na.length = 10;\nconsole.log(a[13]); // undefined, when reducing the length elements after length+1 are removed\nconsole.log(a.length); // 10\n
\n\n

Creating an array using the result of a match

\n\n

The result of a match between a regular expression and a string can create an array.\nThis array has properties and elements that provide information about the match. An\narray is the return value of RegExp.exec, String.match, and String.replace. To help\nexplain these properties and elements, look at the following example and then refer\nto the table below:

\n\n
// Match one d followed by one or more b's followed by one d\n// Remember matched b's and the following d\n// Ignore case\n\nvar myRe = /d(b+)(d)/i;\nvar myArray = myRe.exec(\"cdbBdbsbz\");\n
\n\n

The properties and elements returned from this match are as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Property/Element Description Example
input A read-only property that reflects the original string against which the cdbBdbsbz
regular expression was matched.
index A read-only property that is the zero-based index of the match in the string. 1
[0] A read-only element that specifies the last matched characters. dbBd
[1], ...[n] Read-only elements that specify the parenthesized substring matches, if included in [1]: bB [2]: d
the regular expression. The number of possible parenthesized substrings is unlimited.
\n\n\n
\nDocumentation for this class comes from MDN\nand is available under Creative Commons: Attribution-Sharealike license.\n
\n\n
Defined By

Properties

Reflects the number of elements in an array. ...

Reflects the number of elements in an array.

\n\n

The value of the length property is an integer with a positive sign and a value less than 2 to the 32\npower (232).

\n\n

You can set the length property to truncate an array at any time. When you extend an array by changing\nits length property, the number of actual elements does not increase; for example, if you set length\nto 3 when it is currently 2, the array still contains only 2 elements.

\n\n

In the following example the array numbers is iterated through by looking at the length property to see\nhow many elements it has. Each value is then doubled.

\n\n
var numbers = [1,2,3,4,5];\nfor (var i = 0; i < numbers.length; i++) {\n    numbers[i] *= 2;\n}\n// numbers is now [2,4,6,8,10];\n
\n\n

The following example shortens the array statesUS to a length of 50 if the current length is greater\nthan 50.

\n\n
if (statesUS.length > 50) {\n    statesUS.length=50\n}\n
\n

Methods

Defined By

Instance Methods

new( items ) : Array
Creates new Array object. ...

Creates new Array object.

\n

Parameters

  • items : Number/Object...

    Either a number that specifies the length of array or any number of items\nfor the array.

    \n

Returns

( values ) : Array
Returns a new array comprised of this array joined with other array(s) and/or value(s). ...

Returns a new array comprised of this array joined with other array(s) and/or value(s).

\n\n

concat creates a new array consisting of the elements in the this object on which it is called,\nfollowed in order by, for each argument, the elements of that argument (if the argument is an\narray) or the argument itself (if the argument is not an array).

\n\n

concat does not alter this or any of the arrays provided as arguments but instead returns a\n\"one level deep\" copy that contains copies of the same elements combined from the original arrays.\nElements of the original arrays are copied into the new array as follows:\nObject references (and not the actual object): concat copies object references into the new\narray. Both the original and new array refer to the same object. That is, if a referenced object is\nmodified, the changes are visible to both the new and original arrays.\nStrings and numbers (not String and Number objects): concat copies the values of\nstrings and numbers into the new array.

\n\n

Any operation on the new array will have no effect on the original arrays, and vice versa.

\n\n

Concatenating two arrays

\n\n

The following code concatenates two arrays:

\n\n
var alpha = [\"a\", \"b\", \"c\"];\nvar numeric = [1, 2, 3];\n\n// creates array [\"a\", \"b\", \"c\", 1, 2, 3]; alpha and numeric are unchanged\nvar alphaNumeric = alpha.concat(numeric);\n
\n\n

Concatenating three arrays

\n\n

The following code concatenates three arrays:

\n\n
var num1 = [1, 2, 3];\nvar num2 = [4, 5, 6];\nvar num3 = [7, 8, 9];\n\n// creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged\nvar nums = num1.concat(num2, num3);\n
\n\n

Concatenating values to an array

\n\n

The following code concatenates three values to an array:

\n\n
var alpha = ['a', 'b', 'c'];\n\n// creates array [\"a\", \"b\", \"c\", 1, 2, 3], leaving alpha unchanged\nvar alphaNumeric = alpha.concat(1, [2, 3]);\n
\n

Parameters

  • values : Object...

    Arrays and/or values to concatenate to the resulting array.

    \n

Returns

( callback, [thisObject] ) : Boolean
Tests whether all elements in the array pass the test implemented\nby the provided function. ...

Tests whether all elements in the array pass the test implemented\nby the provided function.

\n\n

every executes the provided callback function once for each element\npresent in the array until it finds one where callback returns a\nfalse value. If such an element is found, the every method\nimmediately returns false. Otherwise, if callback returned a true\nvalue for all elements, every will return true. callback is invoked\nonly for indexes of the array which have assigned values; it is not\ninvoked for indexes which have been deleted or which have never\nbeen assigned values.

\n\n

If a thisObject parameter is provided to every, it will be used as\nthe this for each invocation of the callback. If it is not\nprovided, or is null, the global object associated with callback is\nused instead.

\n\n

every does not mutate the array on which it is called.

\n\n

The range of elements processed by every is set before the first\ninvocation of callback. Elements which are appended to the array\nafter the call to every begins will not be visited by callback. If\nexisting elements of the array are changed, their value as passed\nto callback will be the value at the time every visits them;\nelements that are deleted are not visited.

\n\n

every acts like the \"for all\" quantifier in mathematics. In\nparticular, for an empty array, it returns true. (It is vacuously\ntrue that all elements of the empty set satisfy any given\ncondition.)

\n\n

The following example tests whether all elements in the array are\nbigger than 10.

\n\n
function isBigEnough(element, index, array) {\n  return (element >= 10);\n}\nvar passed = [12, 5, 8, 130, 44].every(isBigEnough);\n// passed is false\npassed = [12, 54, 18, 130, 44].every(isBigEnough);\n// passed is true\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function to test for each element.

    \n

    Parameters

    • value : Mixed

      The element value.

      \n
    • index : Number

      The element index.

      \n
    • array : Array

      The array being traversed.

      \n

    Returns

    • Boolean

      Should return true when element passes the test.

      \n
  • thisObject : Object (optional)

    Object to use as this when executing callback.

    \n

Returns

  • Boolean

    True when all elements pass the test.

    \n
( callback, [thisObject] ) : Array
Creates a new array with all elements that pass the test\nimplemented by the provided function. ...

Creates a new array with all elements that pass the test\nimplemented by the provided function.

\n\n

filter calls a provided callback function once for each element in\nan array, and constructs a new array of all the values for which\ncallback returns a true value. callback is invoked only for indexes\nof the array which have assigned values; it is not invoked for\nindexes which have been deleted or which have never been assigned\nvalues. Array elements which do not pass the callback test are\nsimply skipped, and are not included in the new array.

\n\n

If a thisObject parameter is provided to filter, it will be\nused as the this for each invocation of the callback. If it is not\nprovided, or is null, the global object associated with callback is\nused instead.

\n\n

filter does not mutate the array on which it is called.

\n\n

The range of elements processed by filter is set before the first\ninvocation of callback. Elements which are appended to the array\nafter the call to filter begins will not be visited by callback. If\nexisting elements of the array are changed, or deleted, their value\nas passed to callback will be the value at the time filter visits\nthem; elements that are deleted are not visited.

\n\n

The following example uses filter to create a filtered array that\nhas all elements with values less than 10 removed.

\n\n
function isBigEnough(element, index, array) {\n  return (element >= 10);\n}\nvar filtered = [12, 5, 8, 130, 44].filter(isBigEnough);\n// filtered is [12, 130, 44]\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function to test for each element.

    \n

    Parameters

    • value : Mixed

      The element value.

      \n
    • index : Number

      The element index.

      \n
    • array : Array

      The array being traversed.

      \n

    Returns

    • Boolean

      Should return true when element passes the test.

      \n
  • thisObject : Object (optional)

    Object to use as this when executing callback.

    \n

Returns

  • Array

    Array of elements that passed the test.

    \n
( callback, [thisArg] )
Executes a provided function once per array element. ...

Executes a provided function once per array element.

\n\n

forEach executes the provided function (callback) once for each element present in the array. callback\nis invoked only for indexes of the array which have assigned values; it is not invoked for indexes which\nhave been deleted or which have never been assigned values.

\n\n

If a thisArg parameter is provided to forEach, it will be used as the this value for each callback\ninvocation as if callback.call(thisArg, element, index, array) was called. If thisArg is undefined or\nnull, the this value within the function depends on whether the function is in strict mode or not\n(passed value if in strict mode, global object if in non-strict mode).

\n\n

The range of elements processed by forEach is set before the first invocation of callback. Elements\nwhich are appended to the array after the call to forEach begins will not be visited by callback. If\nexisting elements of the array are changed, or deleted, their value as passed to callback will be the\nvalue at the time forEach visits them; elements that are deleted are not visited.

\n\n

The following code logs a line for each element in an array:

\n\n
function logArrayElements(element, index, array) {\n    console.log(\"a[\" + index + \"] = \" + element);\n}\n[2, 5, 9].forEach(logArrayElements);\n// logs:\n// a[0] = 2\n// a[1] = 5\n// a[2] = 9\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function to execute for each element.

    \n

    Parameters

    • value : Mixed

      The element value.

      \n
    • index : Number

      The element index.

      \n
    • array : Array

      The array being traversed.

      \n
  • thisArg : Object (optional)

    Object to use as this when executing callback.

    \n
( searchElement, [fromIndex] ) : Number
Returns the first index at which a given element can be found in the array, or -1 if it is not present. ...

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

\n\n

indexOf compares searchElement to elements of the Array using strict equality (the same method used\nby the ===, or triple-equals, operator).

\n\n
var array = [2, 5, 9];\nvar index = array.indexOf(2);\n// index is 0\nindex = array.indexOf(7);\n// index is -1\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • searchElement : Mixed

    Element to locate in the array.

    \n
  • fromIndex : Number (optional)

    The index at which to begin the search. Defaults to 0, i.e. the whole array\nwill be searched. If the index is greater than or equal to the length of the array, -1 is returned, i.e.\nthe array will not be searched. If negative, it is taken as the offset from the end of the array. Note\nthat even when the index is negative, the array is still searched from front to back. If the calculated\nindex is less than 0, the whole array will be searched.

    \n

Returns

  • Number

    The index of element found or -1.

    \n
( separator ) : String
Joins all elements of an array into a string. ...

Joins all elements of an array into a string.

\n\n

The string conversions of all array elements are joined into one string.

\n\n

The following example creates an array, a, with three elements, then joins the array three times:\nusing the default separator, then a comma and a space, and then a plus.

\n\n
var a = new Array(\"Wind\",\"Rain\",\"Fire\");\nvar myVar1 = a.join();      // assigns \"Wind,Rain,Fire\" to myVar1\nvar myVar2 = a.join(\", \");  // assigns \"Wind, Rain, Fire\" to myVar2\nvar myVar3 = a.join(\" + \"); // assigns \"Wind + Rain + Fire\" to myVar3\n
\n

Parameters

  • separator : String

    Specifies a string to separate each element of the array. The separator\nis converted to a string if necessary. If omitted, the array elements are separated with a comma.

    \n

Returns

  • String

    A string of the array elements.

    \n
( searchElement, [fromIndex] ) : Number
Returns the last index at which a given element can be found in the array, or -1 if it is not present. ...

Returns the last index at which a given element can be found in the array, or -1 if it is not present.\nThe array is searched backwards, starting at fromIndex.

\n\n

lastIndexOf compares searchElement to elements of the Array using strict equality (the same method\nused by the ===, or triple-equals, operator).

\n\n
var array = [2, 5, 9, 2];\nvar index = array.lastIndexOf(2);\n// index is 3\nindex = array.lastIndexOf(7);\n// index is -1\nindex = array.lastIndexOf(2, 3);\n// index is 3\nindex = array.lastIndexOf(2, 2);\n// index is 0\nindex = array.lastIndexOf(2, -2);\n// index is 0\nindex = array.lastIndexOf(2, -1);\n// index is 3\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • searchElement : Mixed

    Element to locate in the array.

    \n
  • fromIndex : Number (optional)

    The index at which to start searching backwards. Defaults to the array's\nlength, i.e. the whole array will be searched. If the index is greater than or equal to the length of\nthe array, the whole array will be searched. If negative, it is taken as the offset from the end of the\narray. Note that even when the index is negative, the array is still searched from back to front. If\nthe calculated index is less than 0, -1 is returned, i.e. the array will not be searched.

    \n

Returns

  • Number

    The index of element found or -1.

    \n
( callback, [thisObject] ) : Array
Creates a new array with the results of calling a provided function\non every element in this array. ...

Creates a new array with the results of calling a provided function\non every element in this array.

\n\n

map calls a provided callback function once for each element in\nan array, in order, and constructs a new array from the\nresults. callback is invoked only for indexes of the array which\nhave assigned values; it is not invoked for indexes which have been\ndeleted or which have never been assigned values.

\n\n

If a thisArg parameter is provided to map, it will be used as the\nthis for each invocation of the callback. If it is not provided, or\nis null, the global object associated with callback is used\ninstead.

\n\n

map does not mutate the array on which it is called.

\n\n

The range of elements processed by map is set before the first\ninvocation of callback. Elements which are appended to the array\nafter the call to map begins will not be visited by callback. If\nexisting elements of the array are changed, or deleted, their value\nas passed to callback will be the value at the time map visits\nthem; elements that are deleted are not visited.

\n\n

The following code creates an array of \"plural\" forms of nouns from\nan array of their singular forms.

\n\n
function fuzzyPlural(single) {\n  var result = single.replace(/o/g, 'e');\n  if( single === 'kangaroo'){\n    result += 'se';\n  }\n  return result;\n}\n\nvar words = [\"foot\", \"goose\", \"moose\", \"kangaroo\"];\nconsole.log(words.map(fuzzyPlural));\n\n// [\"feet\", \"geese\", \"meese\", \"kangareese\"]\n
\n\n

The following code takes an array of numbers and creates a new\narray containing the square roots of the numbers in the first\narray.

\n\n
var numbers = [1, 4, 9];\nvar roots = numbers.map(Math.sqrt);\n// roots is now [1, 2, 3], numbers is still [1, 4, 9]\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function that produces an element of the new Array\nfrom an element of the current one.

    \n

    Parameters

    • value : Mixed

      The element value.

      \n
    • index : Number

      The element index.

      \n
    • array : Array

      The array being traversed.

      \n

    Returns

    • Boolean

      Should return true when element passes the test.

      \n
  • thisObject : Object (optional)

    Object to use as this when executing callback.

    \n

Returns

  • Array

    Array of the return values of callback function.

    \n
The pop method removes the last element from an array and returns that value to the caller. ...

The pop method removes the last element from an array and returns that value to the caller.

\n\n

pop is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.

\n\n
var myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nvar popped = myFish.pop();\nalert(popped); // Alerts 'surgeon'\n
\n

Returns

  • Object

    The last element in the array

    \n
( elements ) : Number
Adds one or more elements to the end of an array and returns the new length of the array. ...

Adds one or more elements to the end of an array and returns the new length of the array.

\n\n

push is intentionally generic. This method can be called or applied to objects resembling\narrays. The push method relies on a length property to determine where to start inserting\nthe given values. If the length property cannot be converted into a number, the index used\nis 0. This includes the possibility of length being nonexistent, in which case length will\nalso be created.

\n\n

The only native, array-like objects are strings, although they are not suitable in\napplications of this method, as strings are immutable.

\n\n

Adding elements to an array

\n\n

The following code creates the sports array containing two elements, then appends two elements\nto it. After the code executes, sports contains 4 elements: \"soccer\", \"baseball\", \"football\"\nand \"swimming\".

\n\n
var sports = [\"soccer\", \"baseball\"];\nsports.push(\"football\", \"swimming\");\n
\n

Parameters

  • elements : Object...

    The elements to add to the end of the array.

    \n

Returns

  • Number

    The new length property of the object upon which the method was called.

    \n
( callback, [initialValue] ) : Mixed
Applies a function against an accumulator and each value of the\narray (from left-to-right) as to reduce it to a singl...

Applies a function against an accumulator and each value of the\narray (from left-to-right) as to reduce it to a single value.

\n\n

reduce executes the callback function once for each element\npresent in the array, excluding holes in the array.

\n\n

The first time the callback is called, previousValue and\ncurrentValue can be one of two values. If initialValue is\nprovided in the call to reduce, then previousValue will be equal to\ninitialValue and currentValue will be equal to the first value in\nthe array. If no initialValue was provided, then previousValue will\nbe equal to the first value in the array and currentValue will be\nequal to the second.

\n\n

Suppose the following use of reduce occurred:

\n\n
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){\n  return previousValue + currentValue;\n});\n
\n\n

The callback would be invoked four times, with the arguments and\nreturn values in each call being as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
previousValue currentValue index array return value
first call 0 1 1 [0,1,2,3,4] 1
second call 1 2 2 [0,1,2,3,4] 3
third call 3 3 3 [0,1,2,3,4] 6
fourth call 6 4 4 [0,1,2,3,4] 10
\n\n\n

The value returned by reduce would be that of the last callback\ninvocation (10).

\n\n

If you were to provide an initial value as the second argument to\nreduce, the result would look like this:

\n\n
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){\n  return previousValue + currentValue;\n}, 10);\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
previousValue currentValue index array return value
first call 10 0 0 [0,1,2,3,4] 10
second call 10 1 1 [0,1,2,3,4] 11
third call 11 2 2 [0,1,2,3,4] 13
fourth call 13 3 3 [0,1,2,3,4] 16
fifth call 16 4 4 [0,1,2,3,4] 20
\n\n\n

The value returned by reduce this time would be, of course, 20.

\n\n

Example: Sum up all values within an array:

\n\n
var total = [0, 1, 2, 3].reduce(function(a, b) {\n    return a + b;\n});\n// total == 6\n
\n\n

Example: Flatten an array of arrays:

\n\n
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {\n    return a.concat(b);\n});\n// flattened is [0, 1, 2, 3, 4, 5]\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function to execute on each value in the array.

    \n

    Parameters

    • previousValue : Mixed

      The value previously returned in the last\ninvocation of the callback, or initialValue, if supplied.

      \n
    • currentValue : Mixed

      The current element being processed in the array.

      \n
    • index : Number

      The index of the current element being processed in the array.

      \n
    • array : Array

      The array reduce was called upon.

      \n
  • initialValue : Mixed (optional)

    Object to use as the first argument to the first call\nof the callback.

    \n

Returns

  • Mixed

    The value returned by final invocation of the callback.

    \n
( callback, [initialValue] ) : Mixed
Applies a function simultaneously against two values of the array\n(from right-to-left) as to reduce it to a single va...

Applies a function simultaneously against two values of the array\n(from right-to-left) as to reduce it to a single value.

\n\n

reduceRight executes the callback function once for each\nelement present in the array, excluding holes in the array.

\n\n

The first time the callback is called, previousValue and\ncurrentValue can be one of two values. If initialValue is\nprovided in the call to reduceRight, then previousValue will be equal to\ninitialValue and currentValue will be equal to the last value in\nthe array. If no initialValue was provided, then previousValue will\nbe equal to the last value in the array and currentValue will be\nequal to the second-to-last value.

\n\n

Some example run-throughs of the function would look like this:

\n\n
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {\n    return previousValue + currentValue;\n});\n\n// First call\npreviousValue = 4, currentValue = 3, index = 3\n\n// Second call\npreviousValue = 7, currentValue = 2, index = 2\n\n// Third call\npreviousValue = 9, currentValue = 1, index = 1\n\n// Fourth call\npreviousValue = 10, currentValue = 0, index = 0\n\n// array is always the object [0,1,2,3,4] upon which reduceRight was called\n\n// Return Value: 10\n
\n\n

And if you were to provide an initialValue, the result would look like this:

\n\n
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {\n    return previousValue + currentValue;\n}, 10);\n\n// First call\npreviousValue = 10, currentValue = 4, index = 4\n\n// Second call\npreviousValue = 14, currentValue = 3, index = 3\n\n// Third call\npreviousValue = 17, currentValue = 2, index = 2\n\n// Fourth call\npreviousValue = 19, currentValue = 1, index = 1\n\n// Fifth call\npreviousValue = 20, currentValue = 0, index = 0\n\n// array is always the object [0,1,2,3,4] upon which reduceRight was called\n\n// Return Value: 20\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function to execute on each value in the array.

    \n

    Parameters

    • previousValue : Mixed

      The value previously returned in the last\ninvocation of the callback, or initialValue, if supplied.

      \n
    • currentValue : Mixed

      The current element being processed in the array.

      \n
    • index : Number

      The index of the current element being processed in the array.

      \n
    • array : Array

      The array reduceRight was called upon.

      \n
  • initialValue : Mixed (optional)

    Object to use as the first argument to the first call\nof the callback.

    \n

Returns

  • Mixed

    The value returned by final invocation of the callback.

    \n
Reverses the order of the elements of an array -- the first becomes the last, and the\nlast becomes the first. ...

Reverses the order of the elements of an array -- the first becomes the last, and the\nlast becomes the first.

\n\n

The reverse method transposes the elements of the calling array object in place, mutating the\narray, and returning a reference to the array.

\n\n

The following example creates an array myArray, containing three elements, then reverses the array.

\n\n
var myArray = [\"one\", \"two\", \"three\"];\nmyArray.reverse();\n
\n\n

This code changes myArray so that:

\n\n
    \n
  • myArray[0] is \"three\"
  • \n
  • myArray[1] is \"two\"
  • \n
  • myArray[2] is \"one\"
  • \n
\n\n

Returns

  • Array

    A reference to the array

    \n
Removes the first element from an array and returns that element. ...

Removes the first element from an array and returns that element.

\n\n

The shift method removes the element at the zeroeth index and shifts the values at consecutive\nindexes down, then returns the removed value.

\n\n

shift is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.

\n\n

The following code displays the myFish array before and after removing its first element. It also\ndisplays the removed element:

\n\n
// assumes a println function is defined\nvar myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nprintln(\"myFish before: \" + myFish);\nvar shifted = myFish.shift();\nprintln(\"myFish after: \" + myFish);\nprintln(\"Removed this element: \" + shifted);\n
\n\n

This example displays the following:

\n\n
myFish before: angel,clown,mandarin,surgeon\nmyFish after: clown,mandarin,surgeon\nRemoved this element: angel\n
\n

Returns

  • Object

    The first element of the array prior to shifting.

    \n
( begin, end ) : Array
Extracts a section of an array and returns a new array. ...

Extracts a section of an array and returns a new array.

\n\n

slice does not alter the original array, but returns a new \"one level deep\" copy that contains\ncopies of the elements sliced from the original array. Elements of the original array are copied\ninto the new array as follows:\n* For object references (and not the actual object), slice copies object references into the\nnew array. Both the original and new array refer to the same object. If a referenced object\nchanges, the changes are visible to both the new and original arrays.\n* For strings and numbers (not String and Number objects), slice copies strings\nand numbers into the new array. Changes to the string or number in one array does not affect the\nother array.

\n\n

If a new element is added to either array, the other array is not affected.

\n\n

Using slice

\n\n

In the following example, slice creates a new array, newCar, from myCar. Both include a\nreference to the object myHonda. When the color of myHonda is changed to purple, both arrays\nreflect the change.

\n\n
// Using slice, create newCar from myCar.\nvar myHonda = { color: \"red\", wheels: 4, engine: { cylinders: 4, size: 2.2 } };\nvar myCar = [myHonda, 2, \"cherry condition\", \"purchased 1997\"];\nvar newCar = myCar.slice(0, 2);\n\n// Print the values of myCar, newCar, and the color of myHonda\n//  referenced from both arrays.\nprint(\"myCar = \" + myCar.toSource());\nprint(\"newCar = \" + newCar.toSource());\nprint(\"myCar[0].color = \" + myCar[0].color);\nprint(\"newCar[0].color = \" + newCar[0].color);\n\n// Change the color of myHonda.\nmyHonda.color = \"purple\";\nprint(\"The new color of my Honda is \" + myHonda.color);\n\n// Print the color of myHonda referenced from both arrays.\nprint(\"myCar[0].color = \" + myCar[0].color);\nprint(\"newCar[0].color = \" + newCar[0].color);\n
\n\n

This script writes:

\n\n
myCar = [{color:\"red\", wheels:4, engine:{cylinders:4, size:2.2}}, 2, \"cherry condition\",\n\"purchased 1997\"]\nnewCar = [{color:\"red\", wheels:4, engine:{cylinders:4, size:2.2}}, 2]\nmyCar[0].color = red\nnewCar[0].color = red\nThe new color of my Honda is purple\nmyCar[0].color = purple\nnewCar[0].color = purple\n
\n

Parameters

  • begin : Number

    Zero-based index at which to begin extraction.\nAs a negative index, start indicates an offset from the end of the sequence. slice(-2) extracts\nthe second-to-last element and the last element in the sequence

    \n
  • end : Number

    Zero-based index at which to end extraction. slice extracts up to but not\nincluding end.\nslice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3).\nAs a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts\nthe third element through the second-to-last element in the sequence.\nIf end is omitted, slice extracts to the end of the sequence.

    \n

Returns

  • Array

    Array from the new start position up to (but not including) the specified end position.

    \n
( callback, [thisObject] ) : Boolean
Tests whether some element in the array passes the test implemented\nby the provided function. ...

Tests whether some element in the array passes the test implemented\nby the provided function.

\n\n

some executes the callback function once for each element\npresent in the array until it finds one where callback returns a\ntrue value. If such an element is found, some immediately returns\ntrue. Otherwise, some returns false. callback is invoked only for\nindexes of the array which have assigned values; it is not invoked\nfor indexes which have been deleted or which have never been\nassigned values.

\n\n

If a thisObject parameter is provided to some, it will be used as\nthe this for each invocation of the callback. If it is not\nprovided, or is null, the global object associated with callback is\nused instead.

\n\n

some does not mutate the array on which it is called.

\n\n

The range of elements processed by some is set before the first\ninvocation of callback. Elements that are appended to the array\nafter the call to some begins will not be visited by callback. If\nan existing, unvisited element of the array is changed by callback,\nits value passed to the visiting callback will be the value at the\ntime that some visits that element's index; elements that are\ndeleted are not visited.

\n\n

The following example tests whether some element in the array is\nbigger than 10.

\n\n
function isBigEnough(element, index, array) {\n  return (element >= 10);\n}\nvar passed = [2, 5, 8, 1, 4].some(isBigEnough);\n// passed is false\npassed = [12, 5, 8, 1, 4].some(isBigEnough);\n// passed is true\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • callback : Function

    Function to test for each element.

    \n

    Parameters

    • value : Mixed

      The element value.

      \n
    • index : Number

      The element index.

      \n
    • array : Array

      The array being traversed.

      \n

    Returns

    • Boolean

      Should return true when element passes the test.

      \n
  • thisObject : Object (optional)

    Object to use as this when executing callback.

    \n

Returns

  • Boolean

    True when at least one element passes the test.

    \n
( compareFunction ) : Array
Sorts the elements of an array. ...

Sorts the elements of an array.

\n\n

If compareFunction is not supplied, elements are sorted by converting them to strings and\ncomparing strings in lexicographic (\"dictionary\" or \"telephone book,\" not numerical) order. For\nexample, \"80\" comes before \"9\" in lexicographic order, but in a numeric sort 9 comes before 80.

\n\n

If compareFunction is supplied, the array elements are sorted according to the return value of\nthe compare function. If a and b are two elements being compared, then:\nIf compareFunction(a, b) is less than 0, sort a to a lower index than b.\nIf compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but\nsorted with respect to all different elements. Note: the ECMAscript standard does not guarantee\nthis behaviour, and thus not all browsers respect this.\nIf compareFunction(a, b) is greater than 0, sort b to a lower index than a.\ncompareFunction(a, b) must always returns the same value when given a specific pair of elements a\nand b as its two arguments. If inconsistent results are returned then the sort order is undefined

\n\n

So, the compare function has the following form:

\n\n
function compare(a, b)\n{\n    if (a is less than b by some ordering criterion)\n        return -1;\n    if (a is greater than b by the ordering criterion)\n       return 1;\n    // a must be equal to b\n    return 0;\n}\n
\n\n

To compare numbers instead of strings, the compare function can simply subtract b from a:

\n\n
function compareNumbers(a, b)\n{\nreturn a - b;\n}\n
\n\n

The sort() method can be conveniently used with closures:

\n\n
var numbers = [4, 2, 5, 1, 3];\nnumbers.sort(function(a, b) {\n    return a - b;\n});\nprint(numbers);\n
\n

Parameters

  • compareFunction : Function

    Specifies a function that defines the sort order. If omitted, the\narray is sorted lexicographically (in dictionary order) according to the string conversion of each\nelement.

    \n

Returns

  • Array

    A reference to the array

    \n
( index, howMany, elements ) : Array
Adds and/or removes elements from an array. ...

Adds and/or removes elements from an array.

\n\n

If you specify a different number of elements to insert than the number you're removing, the array\nwill have a different length at the end of the call.

\n\n
// assumes a print function is defined\nvar myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nprint(\"myFish: \" + myFish);\n\nvar removed = myFish.splice(2, 0, \"drum\");\nprint(\"After adding 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(3, 1);\nprint(\"After removing 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(2, 1, \"trumpet\");\nprint(\"After replacing 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(0, 2, \"parrot\", \"anemone\", \"blue\");\nprint(\"After replacing 2: \" + myFish);\nprint(\"removed is: \" + removed);\n
\n\n

This script displays:

\n\n
myFish: angel,clown,mandarin,surgeon\nAfter adding 1: angel,clown,drum,mandarin,surgeon\nremoved is:\nAfter removing 1: angel,clown,drum,surgeon\nremoved is: mandarin\nAfter replacing 1: angel,clown,trumpet,surgeon\nremoved is: drum\nAfter replacing 2: parrot,anemone,blue,trumpet,surgeon\nremoved is: angel,clown\n
\n

Parameters

  • index : Number

    Index at which to start changing the array. If negative, will begin that\nmany elements from the end.

    \n
  • howMany : Number

    An integer indicating the number of old array elements to remove. If\nhowMany is 0, no elements are removed. In this case, you should specify at least one new element.\nIf no howMany parameter is specified all elements after index are removed.

    \n
  • elements : Object...

    The elements to add to the array. If you don't specify any\nelements, splice simply removes elements from the array.

    \n

Returns

  • Array

    An array containing the removed elements. If only one element is removed, an array\nof one element is returned..

    \n
Returns a string representing the array and its elements. ...

Returns a string representing the array and its elements. Overrides the Object.prototype.toString\nmethod.

\n\n

The Array object overrides the toString method of Object. For Array objects, the\ntoString method joins the array and returns one string containing each array element separated by\ncommas. For example, the following code creates an array and uses toString to convert the array\nto a string.

\n\n
var monthNames = new Array(\"Jan\",\"Feb\",\"Mar\",\"Apr\");\nmyVar = monthNames.toString(); // assigns \"Jan,Feb,Mar,Apr\" to myVar\n
\n\n

JavaScript calls the toString method automatically when an array is to be represented as a text\nvalue or when an array is referred to in a string concatenation.

\n

Returns

  • String

    The array as a string.

    \n
( elements ) : Number
Adds one or more elements to the front of an array and returns the new length of the array. ...

Adds one or more elements to the front of an array and returns the new length of the array.

\n\n

The unshift method inserts the given values to the beginning of an array-like object.

\n\n

unshift is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.

\n\n

The following code displays the myFish array before and after adding elements to it.

\n\n
// assumes a println function exists\nmyFish = [\"angel\", \"clown\"];\nprintln(\"myFish before: \" + myFish);\nunshifted = myFish.unshift(\"drum\", \"lion\");\nprintln(\"myFish after: \" + myFish);\nprintln(\"New length: \" + unshifted);\n
\n\n

This example displays the following:

\n\n
myFish before: [\"angel\", \"clown\"]\nmyFish after: [\"drum\", \"lion\", \"angel\", \"clown\"]\nNew length: 4\n
\n

Parameters

  • elements : Object...

    The elements to add to the front of the array.

    \n

Returns

  • Number

    The array's new length.

    \n
Defined By

Static Methods

( obj ) : Booleanstatic
Returns true if an object is an array, false if it is not. ...

Returns true if an object is an array, false if it is not.

\n\n
// all following calls return true\nArray.isArray([]);\nArray.isArray([1]);\nArray.isArray( new Array() );\nArray.isArray( Array.prototype ); // Little known fact: Array.prototype itself is an array.\n\n// all following calls return false\nArray.isArray();\nArray.isArray({});\nArray.isArray(null);\nArray.isArray(undefined);\nArray.isArray(17);\nArray.isArray(\"Array\");\nArray.isArray(true);\nArray.isArray(false);\nArray.isArray({ __proto__ : Array.prototype });\n
\n\n

NOTE: This method is part of the ECMAScript 5 standard.

\n

Parameters

  • obj : Mixed

    The object to be checked.

    \n

Returns

","superclasses":[],"meta":{},"requires":[],"html_meta":{},"statics":{"property":[],"cfg":[],"css_var":[],"method":[{"tagname":"method","owner":"Array","meta":{"static":true},"name":"isArray","id":"static-method-isArray"}],"event":[],"css_mixin":[]},"files":[{"href":"Array.html#Array","filename":"Array.js"}],"linenr":1,"members":{"property":[{"tagname":"property","owner":"Array","meta":{},"name":"length","id":"property-length"}],"cfg":[],"css_var":[],"method":[{"tagname":"method","owner":"Array","meta":{},"name":"constructor","id":"method-constructor"},{"tagname":"method","owner":"Array","meta":{},"name":"concat","id":"method-concat"},{"tagname":"method","owner":"Array","meta":{},"name":"every","id":"method-every"},{"tagname":"method","owner":"Array","meta":{},"name":"filter","id":"method-filter"},{"tagname":"method","owner":"Array","meta":{},"name":"forEach","id":"method-forEach"},{"tagname":"method","owner":"Array","meta":{},"name":"indexOf","id":"method-indexOf"},{"tagname":"method","owner":"Array","meta":{},"name":"join","id":"method-join"},{"tagname":"method","owner":"Array","meta":{},"name":"lastIndexOf","id":"method-lastIndexOf"},{"tagname":"method","owner":"Array","meta":{},"name":"map","id":"method-map"},{"tagname":"method","owner":"Array","meta":{},"name":"pop","id":"method-pop"},{"tagname":"method","owner":"Array","meta":{},"name":"push","id":"method-push"},{"tagname":"method","owner":"Array","meta":{},"name":"reduce","id":"method-reduce"},{"tagname":"method","owner":"Array","meta":{},"name":"reduceRight","id":"method-reduceRight"},{"tagname":"method","owner":"Array","meta":{},"name":"reverse","id":"method-reverse"},{"tagname":"method","owner":"Array","meta":{},"name":"shift","id":"method-shift"},{"tagname":"method","owner":"Array","meta":{},"name":"slice","id":"method-slice"},{"tagname":"method","owner":"Array","meta":{},"name":"some","id":"method-some"},{"tagname":"method","owner":"Array","meta":{},"name":"sort","id":"method-sort"},{"tagname":"method","owner":"Array","meta":{},"name":"splice","id":"method-splice"},{"tagname":"method","owner":"Array","meta":{},"name":"toString","id":"method-toString"},{"tagname":"method","owner":"Array","meta":{},"name":"unshift","id":"method-unshift"}],"event":[],"css_mixin":[]},"inheritable":null,"private":null,"component":false,"name":"Array","singleton":false,"override":null,"inheritdoc":null,"id":"class-Array","mixins":[],"mixedInto":[]});