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.
typedarray.reduce(callback[, initialValue])
callback
previousValue
initialValue
, if supplied (see below).currentValue
index
array
reduce
was called upon.initialValue
callback
.The value that results from the reduction.
The reduce
method executes the callback
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 callback
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, previousValue
and currentValue
can be one of two values. If initialValue
is provided in the call to reduce
, then previousValue
will be equal to initialValue
and currentValue
will be equal to the first value in the typed array. If no initialValue
was provided, then previousValue
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 callback
.
var total = new Uint8Array([0, 1, 2, 3]).reduce(function(a, b) { return a + b; }); // total == 6
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of '%TypedArray%.prototype.reduce' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of '%TypedArray%.prototype.reduce' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 45 | 14 | 37 | No | 32 | 10 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | ? | Yes | 37 | No | 10 | ? |
Server | |
---|---|
Node.js | |
Basic support | 4.0.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/reduce