TypedArray.prototype.reduce()
The reduce()
method applies a function against an
accumulator and each value of the typed array (from left-to-right) has to reduce it to a
single value. This method has the same algorithm as
Array.prototype.reduce()
. TypedArray is one of the
typed array types here.
Try it
Syntax
reduce(callbackFn)
reduce(callbackFn, initialValue)
Parameters
callbackFn
-
A function to execute for each element in the typed array. Its return value becomes the value of the
accumulator
parameter on the next invocation ofcallbackFn
. For the last invocation, the return value becomes the return value ofreduce()
. The function is called with the following arguments:accumulator
-
The value resulting from the previous call to
callbackFn
. On first call,initialValue
if specified, otherwise the value ofarray[0]
. currentValue
-
The value of the current element. On first call, the value of
array[0]
if aninitialValue
was specified, otherwise the value ofarray[1]
. currentIndex
-
The index position of
currentValue
in the typed array. On first call,0
ifinitialValue
was specified, otherwise1
. array
-
The typed array
reduce()
was called upon.
initialValue
Optional-
A value to which
accumulator
is initialized the first time the callback is called. IfinitialValue
is specified,callbackFn
starts executing with the first value in the typed array ascurrentValue
. IfinitialValue
is not specified,accumulator
is initialized to the first value in the typed array, andcallbackFn
starts executing with the second value in the typed array ascurrentValue
. In this case, if the typed array is empty (so that there's no first value to return asaccumulator
), an error is thrown.
Return value
The value that results from the reduction.
Description
The reduce
method executes the callbackFn
function
once for each element present in the typed array, excluding holes in the typed array,
receiving four arguments: the initial value (or value from the previous
callbackFn
call), the value of the current element, the current
index, and the typed array over which iteration is occurring.
The first time the callback is called, accumulator
and
currentValue
can be one of two values. If
initialValue
is provided in the call to reduce
,
then accumulator
will be equal to
initialValue
and currentValue
will be
equal to the first value in the typed array. If no initialValue
was provided, then accumulator
will be equal to the first
value in the typed array and currentValue
will be equal to the
second.
If the typed array is empty and no initialValue
was provided,
TypeError
would be thrown. If the typed array has only one element
(regardless of position) and no initialValue
was provided, or if
initialValue
is provided but the typed array is empty, the solo
value would be returned without calling callbackFn
.
Examples
Sum up all values within an array
const total = new Uint8Array([0, 1, 2, 3]).reduce((a, b) => a + b);
// total === 6
Specifications
Specification |
---|
ECMAScript Language Specification # sec-%typedarray%.prototype.reduce |
Browser compatibility
BCD tables only load in the browser