Thursday, October 16, 2025
HomeLanguagesJavascriptUnderscore.js _.iterators.mapcat() Method

Underscore.js _.iterators.mapcat() Method

With the help of _.iterators.mapcat() method, we can get the function iterator which when called return the value which is  flattening the contents of iterator and combined with the unary function by using this method.

Syntax:

_.iterators.mapcat(iter, unaryFn)

Parameters: This method accepts two parameter as mentioned above and described below: 
 

  • iter: This parameter holds the iterator list of the array.
  • unaryFn: This parameter holds the unary function key.

 

Return value: Return the function iterator which generate the value by mapping with unaryFn.

 

Note: To execute the below examples, you have to install the underscore-contrib library by using this command prompt we have to execute the following command.

 

npm install underscore-contrib

Below example illustrate the Underscore.js _.iterators.mapcat() method in JavaScript:
 

Example 1: In this example, we can see that by using _.iterators.mapcat() method, we are able to get the function iterator which can generate the value after mapping with the unary function by using this method.

 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib');
 
function generateNumber (x)  {
    return _.iterators.List(_.range(1, x));
}
 
var treeIter = _.iterators.Tree([2, [3]]);
 
var geek = _.iterators.mapcat(treeIter, generateNumber);
 
geek();
geek();
geek();


Output :

1
1
2

Example 2:

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib');
 
function generateNumber (x)  {
    return _.iterators.List(_.range(x));
}
 
var treeIter = _.iterators.Tree([[2, 1], [1, 3]]);
 
var geek = _.iterators.mapcat(treeIter, generateNumber);
 
for(var i = 0; i < 5; i++) {
    console.log(geek());
}


Output:

0
1
0
0
0
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS