Thursday, January 29, 2026
HomeLanguagesJavascriptUnderscore.js _.reductions() Method

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.

Syntax:

_.reductions(array, function, start_val)

Parameters: 

  • 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.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.  Underscore.js contrib library can be installed using 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.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Defining Array
var array = [10, 12, 23, 34, 45];
  
var arr =_.reductions(array, function(st, n) {
  return st + n;
}, 0);
console.log("Generated Array : ");
console.log(arr);


Output:

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.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Defining Array
var array = [10, 12, 23, 34, 45];
  
var arr =_.reductions(array, function(st, n) {
  return st * n;
}, 1);
console.log("Generated Array : ");
console.log(arr);


Output:

Generated Array :
[ 10, 120, 2760, 93840, 4222800 ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32477 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6916 POSTS0 COMMENTS