Open In App

Underscore.js _.iterators.map() method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

With the help of _.iterators.map() method, we can get the function of the new iterator which will return the value of the 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.

javascript




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


Output :

'Geeks'
'for'

Example 2:

javascript




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


Output:

'A1'
'ABA3'
'ABCBA5'


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads