Underscore.js _.mapArgsWith() Method

The _.mapArgsWith() method takes a mapping function and returns a new combinator function which will take a target function and will return a new function that maps its arguments with the mapping function before executing the body of the target function.
Syntax:
_.mapArgsWith( mapping_function );
Parameters:
- mapping_function: Mapping function to be accepted by the function.
Return Value: This method returns a function.
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1: We made a function that cubes the given value then adds that value to itself.
Javascript
// Defining underscore contrib variable var _ = require('underscore-contrib'); function add (x) { return x + x + x ; } function sub (x) { return x - 2; } var addnow = _.mapArgsWith(sub); var subnow = addnow(add); console.log(subnow(5)) |
Output:
9
Example 2:
Javascript
// Defining underscore contrib variable var _ = require('underscore-contrib'); function squ (x) { return x * x ; } function add (x) { return x + 10; } var addnow = _.mapArgsWith(add); var sq = addnow(squ); console.log(sq(5)) |
Output:
225
Example 3:
Javascript
// Defining underscore contrib variable var _ = require('underscore-contrib'); function cs (x) { return "zambiatek : Computer Science Portal for Geeks"; } function geek (x) { return "zambiatek"; } var gfg = _.mapArgsWith(geek); var gfgFunc = gfg(cs); console.log(gfgFunc()) |
Output:
zambiatek : Computer Science Portal for Geeks
Example 4:
Javascript
// Defining underscore contrib variable var _ = require('underscore-contrib'); function cs (x) { return x; } function geek (x) { return x[0]+" : "+x[1]; } var gfg = _.mapArgsWith(geek); var gfgFunc = gfg(cs); console.log(gfgFunc(["zambiatek", "Computer Science Portal for Geeks"])) |
Output:
zambiatek : Computer Science Portal for Geeks
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



