Reduce an array (from right to left) to a single value.
This method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value..
Kind: Exported member
Returns: *
- The value that results from the reduction.
Throws:
TypeError
If array is null or undefined.TypeError
If callBack is not a function.TypeError
If called on an empty array without an initial value.
Param | Type | Description |
---|---|---|
array | array |
The array to iterate over. |
callBack | function |
Function to execute for each element. |
[initialValue] | * |
Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduceRight on an empty array without an initial value is an error. |
Example
import reduceRight from 'array-reduce-right-x';
console.log(
reduceRight(
[0, 1, 2, 3],
function(a, b) {
return a + b;
},
0,
),
); // sum is 6