Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Underscore.js _.iterators.map() method

  • Last Updated : 09 Sep, 2020

With the help of _.iterators.map() method, we can get the function of new iterator which will return the value of unary function which uses the values of the List iterator by using this method.

Syntax:

_.iterators.map(iter, unaryFn)

Return: Return the value from the new iterator function.

In the examples given below we are just showing the implementation part, you can use it anywhere as you want.

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

Example 1:

In this example we can see that by using _.iterators.map() method, we are able to get the value from the new iterator function which uses the unary function to generate values by using this method.




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var iter = _.iterators.List(["Geek", "for", "Geek"]);
  
function postfixGeek (val) {
    if(val == "Geek") {
        return val + "s";
    }
    return val;
}
  
var geek = _.iterators.map(iter, postfixGeek);
  
geek();
geek();

Output :

'Geeks'
'for'

Example 2:




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var iter = _.iterators.List(["A", "ABA", "ABCBA"]);
  
function postfixLength (val) {
    return val + String(val.length);
}
  
var geek = _.iterators.map(iter, postfixLength);
  
geek();
geek();
geek();

Output:

'A1'
'ABA3'
'ABCBA5'


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!