Monday, December 15, 2025
HomeLanguagesJavascriptUnderscore.js _.iterators.accumulate() method

Underscore.js _.iterators.accumulate() method

With the help of _.iterators.accumulate(iter, binaryFn) method, we can get the new iterator function which when called will iterate the next step with iter and generate the value by using binaryFn.

Syntax: 

_.iterators.accumulate(iter, binaryFn)

Return: Return the iterator function which will generally result in an accumulated Binary function.

Example 1: In this example, we can see that by using _.iterators.accumulate(iter, binaryFn) method, we are able to get the function iterator which will return the value by using the accumulated binary function.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
 
let iter = _.iterators.List(["Geeks", "for", "Geeks"]);
 
function calculateLength(geeky, element) {
    return element.length;
}
 
let gfg = _.iterators.accumulate(iter, calculateLength, 0);
 
for (let i = 0; i < 3; i++) {
    console.log(gfg());
}


Output:

5
3
5

Example 2: In this example, we will see the use of  _.iterators.accumulate(iter, binaryFn) method.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
 
let iter = _.iterators.List(["A", "AB", "ABC", "AB", "A"]);
 
function calculateLength(geeky, element) {
    return element.length;
}
 
let gfg = _.iterators.accumulate(iter, calculateLength, 0);
 
for (let i = 0; i < 5; i++) {
    console.log(gfg());
}


Output:

1
2
3
2
1
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
32448 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6817 POSTS0 COMMENTS
Nicole Veronica
11954 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6955 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6883 POSTS0 COMMENTS