Underscore.js _.reductions() Method
The _.reductions() method is used to transform an array of elements to an array in which every intermediate value in the folding operation is stored. This method is the same as _.reduce() method except it returns an array.
An array, a function, and a starting value are passed in this method to generate a new array to perform operations on the array.
_.reductions(array, function, start_val)
- array: The array to be work upon.
- function: The function containing iteration conditions.
- start_val: The value passed at starting which further updates on further operations.
Return Value: This method returns a new array.
npm install underscore-contrib --save.
Example 1: In this example, we will generate an array using this method. Here, the sum array is generated with starting value given as 0 which updates on addition operations.
Generated Array : [ 10, 22, 45, 79, 124 ]
Example 2: In this example, we will generate a multiplication array by giving starting value as 1 which updates on further multiplication.
Generated Array : [ 10, 120, 2760, 93840, 4222800 ]