Typed Array reduce()
Examples
Subtract all numbers in an array:
// Reducer Function
function myFunc(total, num) {
return total - num;
}
// Create a Typed Array
const myArr = Int32Array.of(40, 100, 1, 5, 25, 10);
// Reduce the Array to a Number
let number = myArr.reduce(myFunc);
Try it Yourself »
Description
The reduce()
method executes a function for each array element.
The reduce()
method returns a single value: accumulated result.
The reduce()
method does not execute the function for empty array elements.
The reduce()
method does not change the original array.
Note
At the first callback, there is no return value from the previous callback.
Normally, array element 0 is used as initial value, and the iteration starts from array element 1.
If an initial value is supplied, this is used, and the iteration starts from array element 0.
See Also:
Syntax
typed-array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Parameters
Parameter | Description | ||||||||
function() | Required. A function to be run for each element in the array. |
||||||||
Reducer function parameters:
| |||||||||
initialValue | Optional. A value to be passed to the function as the initial value. |
Return Value
The accumulated result from the last call of the callback function. |
JavaScript Typed Arrays
Browser Support
typed-array.reduce()
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is fully supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |