Array

Overview

The javascript Array implementation. It is a collection of items kept by index, providing typical array API. The index is a integer (>= 0) or a string representing such an integer.

For more information see: Array (MDN).

Properties Summarized

Type
Name
Summary

Get an element by index.

Get the length of the array.

Methods Summarized

Type
Name
Summary

Takes an integer value and returns the item at that index, allowing for positive and negative integers.

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).

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

Shallow copies part of an array to another location in the same array and returns it without modifying its length

Shallow copies part of an array to another location in the same array and returns it without modifying its length

Shallow copies part of an array to another location in the same array and returns it without modifying its length

The entries() method of Array instances returns a new array iterator object that contains the key/value pairs for each index in the array.

Runs a function on items in the array while that function is returning true.

Runs a function on items in the array while that function is returning true.

Changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.

Changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.

Changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.

Runs a function on every item in the array and returns an array of all items for which the function returns true.

Runs a function on every item in the array and returns an array of all items for which the function returns true.

Returns the value of the first element in the provided array that satisfies the provided testing function.

Returns the index of the first element in the provided array which satisfies the provided testing function.

Iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function.

Iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function.

The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.

The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.

void

Runs a function (callback) on every item in the array.

void

Runs a function (callback) on every item in the array.

Creates a new, shallow-copied Array instance from an iterable or array-like object.

Creates a new, shallow-copied Array instance from an iterable or array-like object.

Determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Determines whether an array includes a certain value among its entries, returning true or false as appropriate.

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.

Checks whether an object is an array or not.

Puts all elements in the array into a string, separating each element with the specified delimiter

It is the copying version of using the bracket notation to change the value of a given index.

The keys() method of Array instances returns a new array iterator object that contains the keys for each index in the array.

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.

Runs a function on every item in the array and returns the results in an array.

Runs a function on every item in the array and returns the results in an array.

Ccreates a new Array instance from a variable number of arguments.

Pops the last string off the array and returns it.

Mutates an array by appending the given elements and returning the new length of the array.

Mutates an array by appending the given elements and returning the new length of the array.

Mutates an array by appending the given elements and returning the new length of the array.

Reduces the array to a single value by executing a provided function for each value of the array (from left-to-right).

The reduceRight() method of Array instances applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

The reduceRight() method of Array instances applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

Puts array elements in reverse order.

Decreases array element size by one by shifting the first element off the array and returning it.

The slice method creates a new array from a selected section of an array.

The slice method creates a new array from a selected section of an array.

Runs a function on items in the array while that function returns false.

Runs a function on items in the array while that function returns false.

Sorts the array elements in dictionary order or using a compare function passed to the method.

Sorts the array elements in dictionary order or using a compare function passed to the method.

It is used to take elements out of an array and replace them with those specified.

It is used to take elements out of an array and replace them with those specified.

It is used to take elements out of an array and replace them with those specified.

It is used to take elements out of an array and replace them with those specified.

The toReversed() method transposes the elements of the calling array object in reverse order and returns a new array.

It is the copying version of the sort() method.

It is the copying version of the sort() method.

It is the copying version of the splice() method.

It is the copying version of the splice() method.

It is the copying version of the splice() method.

It is the copying version of the splice() method.

Places element data at the start of an array.

The values() method of Array instances returns a new array iterator object that iterates the value of each item in the array.

Properties Detailed

[index]

Get an element by index.

Type Object

Sample

length

Get the length of the array.

Type Number

Sample

Methods Detailed

at(index)

Takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

Parameters

Returns: Array The element in the array matching the given index.

Sample

concat(value1)

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

Parameters

Returns: Array

Sample

concat(value1, value2)

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

Parameters

Returns: Array

Sample

concat(value1, value2, valueN)

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

Parameters

Returns: Array

Sample

copyWithin(target)

Shallow copies part of an array to another location in the same array and returns it without modifying its length

Parameters

  • Number target Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.

Returns: Array

Sample

copyWithin(target, start)

Shallow copies part of an array to another location in the same array and returns it without modifying its length

Parameters

  • Number target Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.

  • Number start Zero-based index at which to start copying elements from. If negative, start will be counted from the end. If start is omitted, copyWithin will copy from index 0.

Returns: Array

Sample

copyWithin(target, start, end)

Shallow copies part of an array to another location in the same array and returns it without modifying its length

Parameters

  • Number target Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.

  • Number start Zero-based index at which to start copying elements from. If negative, start will be counted from the end. If start is omitted, copyWithin will copy from index 0.

  • Number end Zero-based index at which to end copying elements from. copyWithin copies up to but not including end. If negative, end will be counted from the end.

Returns: Array

Sample

entries()

The entries() method of Array instances returns a new array iterator object that contains the key/value pairs for each index in the array.

Returns: Iterator the iterator

Sample

every(callback)

Runs a function on items in the array while that function is returning true. It returns true if the function returns true for every item it could visit. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Boolean

Sample

every(callback, thisObject)

Runs a function on items in the array while that function is returning true. It returns true if the function returns true for every item it could visit. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Boolean

Sample

fill(value)

Changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

Parameters

  • Object value Value to fill the array with.

Returns: Array

Sample

fill(value, start)

Changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

Parameters

  • Object value Value to fill the array with.

  • Number start Zero-based index at which to start filling.

Returns: Array

Sample

fill(value, start, end)

Changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

Parameters

  • Object value Value to fill the array with.

  • Number start Zero-based index at which to start filling.

  • Number end Zero-based index at which to end filling.

Returns: Array

Sample

filter(callback)

Runs a function on every item in the array and returns an array of all items for which the function returns true. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Array

Sample

filter(callback, thisObject)

Runs a function on every item in the array and returns an array of all items for which the function returns true. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Array

Sample

find(callback)

Returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. The callback function can invoked with three arguments: the element value, the element index(optional), the array being traversed (optional).

Parameters

Returns: Object the element which satisfies the function or undefined

Sample

findIndex(callback)

Returns the index of the first element in the provided array which satisfies the provided testing function. If no values satisfy the testing function, -1 is returned. The callback function can invoked with three arguments: the element value, the element index (optional), the array being traversed (optional).

Parameters

Returns: Number the index of the first element which satisfies the function or -1

Sample

findLast(callback)

Iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned. The callback function can invoked with three arguments: the element value, the element index (optional), the array being traversed (optional).

Parameters

Returns: Object the index of the first element which satisfies the function or undefined

Sample

findLastIndex(callback)

Iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. The callback function can invoked with three arguments: the element value, the element index (optional), the array being traversed (optional).

Parameters

Returns: Object the index of the first element which satisfies the function or undefined

Sample

flat()

The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. Default depth is 1.

Returns: Array new flat array

Sample

flat(depth)

The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Parameters

  • Number depth The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.

Returns: Array new flat array

Sample

flatMap(callback)

The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1, but slightly more efficient than calling those two methods separately.

Parameters

  • Function callback ``` A function that produces an element of the new Array, taking three arguments: - element The current element being processed in the array. - index The index of the current element being processed in the array. - array The array flatMap() was called upon.

flatMap(callback, thisObject)

The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1, but slightly more efficient than calling those two methods separately.

Parameters

  • Function callback ``` A function that produces an element of the new Array, taking three arguments: - element The current element being processed in the array. - index The index of the current element being processed in the array. - array The array flatMap() was called upon.

forEach(callback)

Runs a function (callback) on every item in the array. The callback function is invoked only for indexes of the array which have assigned values. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: void

Sample

forEach(callback, thisObject)

Runs a function (callback) on every item in the array. The callback function is invoked only for indexes of the array which have assigned values. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: void

Sample

from(value)

Creates a new, shallow-copied Array instance from an iterable or array-like object.

Parameters

  • Object value An iterable or array-like object to convert to an array.

Returns: Array

Sample

from(value, mapFunction, thisObject)

Creates a new, shallow-copied Array instance from an iterable or array-like object.

Parameters

  • Object value An iterable or array-like object to convert to an array.

  • Function mapFunction Map function to call on every element of the array. If provided, every value to be added to the array is first passed through this function, and mapFunction's return value is added to the array instead.

  • Object thisObject Value to use as this when executing mapFunction.

Returns: Array

Sample

includes(searchElement)

Determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Parameters

  • Object searchElement The value to search for.

Returns: Boolean

Sample

includes(searchElement, start)

Determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Parameters

  • Object searchElement The value to search for.

  • Number start Zero-based index at which to start searching.

Returns: Boolean

Sample

indexOf(searchElement)

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

Parameters

Returns: Number

Sample

indexOf(searchElement, fromIndex)

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

Parameters

Returns: Number

Sample

isArray(obj)

Checks whether an object is an array or not.

Parameters

Returns: Boolean

Sample

join(delimiter)

Puts all elements in the array into a string, separating each element with the specified delimiter

Parameters

Returns: String

Sample

js_with(index, value)

It is the copying version of using the bracket notation to change the value of a given index.

Parameters

Returns: Array a new array with the element at the given index replaced with the given value.

Sample

keys()

The keys() method of Array instances returns a new array iterator object that contains the keys for each index in the array.

Returns: Iterator the iterator

Sample

lastIndexOf(searchElement)

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 fromIndex.

Parameters

Returns: Number

Sample

lastIndexOf(searchElement, fromIndex)

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 fromIndex.

Parameters

Returns: Number

Sample

map(callback)

Runs a function on every item in the array and returns the results in an array. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Array

Sample

map(callback, thisObject)

Runs a function on every item in the array and returns the results in an array. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Array

Sample

of(value)

Ccreates a new Array instance from a variable number of arguments.

Parameters

Returns: Array

Sample

pop()

Pops the last string off the array and returns it.

Returns: Object

Sample

push(value1)

Mutates an array by appending the given elements and returning the new length of the array.

Parameters

Returns: Number

Sample

push(value1, value2)

Mutates an array by appending the given elements and returning the new length of the array.

Parameters

Returns: Number

Sample

push(value1, value2, valueN)

Mutates an array by appending the given elements and returning the new length of the array.

Parameters

Returns: Number

Sample

reduce(f, initialValue)

Reduces the array to a single value by executing a provided function for each value of the array (from left-to-right).

Parameters

  • Function f Function to execute on each element in the array, taking four arguments: -accumulator: accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied (see below). -currentValue: the current element being processed in the array. -currentIndex (Optional): the index of the current element being processed in the array (starts at index 0, if an initialValue is provided, and at index 1 otherwise) -array (Optional): the array reduce() was called upon.

  • Object initialValue Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.

Returns: Object Object

Sample

reduceRight(f)

The reduceRight() method of Array instances applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. See also reduce() for left-to-right processing.

Parameters

  • Function f Function to execute on each element in the array, taking four arguments: -accumulator: accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied (see below). -currentValue: the current element being processed in the array. -currentIndex: the index of the current element being processed in the array (starts at index 0, if an initialValue is provided, and at index 1 otherwise) -array : the array reduce() was called upon.

Returns: Object Object The value that results from the reduction.

Sample

reduceRight(f, initialValue)

The reduceRight() method of Array instances applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. See also reduce() for left-to-right processing.

Parameters

  • Function f Function to execute on each element in the array, taking four arguments: -accumulator: accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied (see below). -currentValue: the current element being processed in the array. -currentIndex: the index of the current element being processed in the array (starts at index 0, if an initialValue is provided, and at index 1 otherwise) -array : the array reduce() was called upon.

  • Object initialValue Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.

Returns: Object Object The value that results from the reduction.

Sample

reverse()

Puts array elements in reverse order.

Returns: Array

Sample

shift()

Decreases array element size by one by shifting the first element off the array and returning it.

Returns: Object

Sample

slice(begin)

The slice method creates a new array from a selected section of an array.

Parameters

Returns: Array

Sample

slice(begin, end)

The slice method creates a new array from a selected section of an array.

Parameters

Returns: Array

Sample

some(callback)

Runs a function on items in the array while that function returns false. It returns true if the function returns true for any item it could visit. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Boolean

Sample

some(callback, thisObject)

Runs a function on items in the array while that function returns false. It returns true if the function returns true for any item it could visit. The callback function is invoked with three arguments: the element value, the element index, the array being traversed.

Parameters

Returns: Boolean

Sample

sort()

Sorts the array elements in dictionary order or using a compare function passed to the method.

Returns: Array

Sample

sort(function)

Sorts the array elements in dictionary order or using a compare function passed to the method.

Parameters

Returns: Array

Sample

splice(arrayIndex, length)

It is used to take elements out of an array and replace them with those specified.

Parameters

Returns: Array

Sample

splice(arrayIndex, length, value1)

It is used to take elements out of an array and replace them with those specified.

Parameters

Returns: Array

Sample

splice(arrayIndex, length, value1, value2)

It is used to take elements out of an array and replace them with those specified.

Parameters

Returns: Array

Sample

splice(arrayIndex, length, value1, value2, valueN)

It is used to take elements out of an array and replace them with those specified.

Parameters

Returns: Array

Sample

toReversed()

The toReversed() method transposes the elements of the calling array object in reverse order and returns a new array. When used on sparse arrays, the toReversed() method iterates empty slots as if they have the value undefined.

Returns: Array a new array containing the elements in reversed order

Sample

toSorted()

It is the copying version of the sort() method. It returns a new array with the elements sorted in ascending order. The array elements are converted to strings, then sorted according to each character's Unicode code point value. When used on sparse arrays, the toSorted() method iterates empty slots as if they have the value undefined.

Returns: Array a new array containing the elements sorted in ascending order

Sample

toSorted(comparator)

It is the copying version of the sort() method. It returns a new array with the elements sorted in ascending order. When used on sparse arrays, the toSorted() method iterates empty slots as if they have the value undefined.

Parameters

  • Function comparator a comparator function

Returns: Array a new array containing the elements sorted in ascending order

Sample

toSpliced(start)

It is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.

Parameters

Returns: Array a new array that consists of all elements before start

Sample

toSpliced(start, deleteCount)

It is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.

Parameters

Returns: Array a new array that consists of all elements before start and all elements after start + deleteCount.

Sample

toSpliced(start, deleteCount, item1)

It is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.

Parameters

Returns: Array a new array that consists of all elements before start, adds item1 to the array (beginning from start) and all elements after start + deleteCount.

Sample

toSpliced(start, deleteCount, items)

It is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.

Parameters

Returns: Array a new array that consists of all elements before start, adds items to the array (beginning from start) and all elements after start + deleteCount.

Sample

unshift(value1, value2, valueN)

Places element data at the start of an array.

Parameters

Returns: Number

Sample

values()

The values() method of Array instances returns a new array iterator object that iterates the value of each item in the array.

Returns: Iterator the iterator

Sample


Last updated

Was this helpful?