Array Prototype Methods
The following methods are applied to instances of
fill method mutates (changes) the array instance to which it is applied, filling all elements (or some portion of the elements, as dictated by the
end parameters) with the specified
Array.prototype.fill(value, start?, end?)
start value is the index from which to start filling; the
end value is the index up to which to (but not including) to end filling. Note that indexing here is 0-based (the first element has index 0), and that the optional
end? parameters default to 0 and the length of the array.
start value is interpreted as
length + start. Similarly, a negative
end value is interpreted as
length + end.
The following example illustrates the use of
filter method returns a new array from the array to which it is applied (without changing the original array) where each element passes a test, supplied to the method as a callback function:
The function passed as
callbackFunction should return
false as it is applied to each element. Optionally, we can supply a value for
callbackFunction to use as
find method returns the first element from the array for which the callback function returns
true; if no such element exists, then
The method takes an optional second parameter to use as
this when executing
In the next set of examples, we demonstrate how to use the
find method with an array of objects:
The array method
forEach gives us a way to apply a callback function to each element of the array:
The method takes an optional second parameter - the value to use as
this for the callback function.
The callback function specified by the
callbackFunction parameter takes three arguments: the current element, the index of the current element, and the array to which
forEach is being applied.
Note that there is no way to break out of a
for loop or similar construct if this behavior is needed.
The following example shows the use of the
indexOf method returns the first index at which the specified element is found in the array to which it is applied, or -1 is the element is not found:
We can pass an optional parameter to specify the index at which to start the search; passing a
start parameter which is greater than or equal to the length of the array returns -1.
In the following example, we use
indexOf to search an array and to build a function which returns the unduplicated elements of an array:
The array method
keys returns a new
Array Iterator with the keys from each index of the array to which it was applied. It does not change the array on which it is called:
Note that the
keys method does not ignore "holes" in the array, as in the array
['one', , 'three']. The following example shows the use of
map method returns a new array from the application of its callback-function parameter to each element of the original array:
Optionally, we can supply a value for the callback function to use as
The callback function is excuted on each element of the array in order and takes three arguments: the current element, the current index, and the original array to which
map is being applied.
In the following example, we use the
map method to round an array of numbers and to produce an array of full names from an array of objects:
The reduce method applies a callback function once for each value of the array, offering a means to return a single, calculated value - the sum of all elements, for example - from the array:
The callback function executed by
reduce takes four arguments:
previousVal: The value previously returned by the last invokation of the callback function, or the
initalValue parameter, if present.
currentVal: The current element being processed.
currentInd: The index of the current element being processed.
arr: The array upon which
reduce was called.
If you apply
reduce to an array and do not supply an
initialValue parameter, then
previousVal will be equal to the first value of the array and
currentVal will be equal to the second value of the array.
Let's look at a few examples to better explain the use of
We'll ask you to try out Array methods in the next exercise: