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.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
typedarray.reduce(callback[, initialValue])
callback
previousValue
initialValue
, if supplied (see below).
currentValue
The current element being processed in the typed array.
index
The index of the current element being processed in the typed array.
array
reduce
was called upon.
initialValue
callback
.
The value that results from the reduction.
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
.
This method uses the same algorithm as
Array.prototype.reduce()
, so the same polyfill can be used here: simply replace
Array.prototype.reduce
with
TypedArray.prototype.reduce
.
var total = new Uint8Array([0, 1, 2, 3]).reduce(function(a, b) {
return a + b;
});
// total == 6
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '%TypedArray%.prototype.reduce' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reduce
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
完整支持
不支持
TypedArray
TypedArray.from()
TypedArray.of()
TypedArray.prototype.copyWithin()
TypedArray.prototype.entries()
TypedArray.prototype.every()
TypedArray.prototype.fill()
TypedArray.prototype.filter()
TypedArray.prototype.find()
TypedArray.prototype.findIndex()
TypedArray.prototype.forEach()
TypedArray.prototype.includes()
TypedArray.prototype.indexOf()
TypedArray.prototype.join()
TypedArray.prototype.keys()
TypedArray.prototype.lastIndexOf()
TypedArray.prototype.map()
TypedArray.prototype.reduce()
TypedArray.prototype.reduceRight()
TypedArray.prototype.reverse()
TypedArray.prototype.set()
TypedArray.prototype.slice()
TypedArray.prototype.some()
TypedArray.prototype.sort()
TypedArray.prototype.subarray()
TypedArray.prototype.toLocaleString()
TypedArray.prototype.toString()
TypedArray.prototype.values()
TypedArray.prototype[@@iterator]()
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()