Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.iterators.map() method

Underscore.js _.iterators.map() method

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'

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

Recent Comments