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 "neveropen : Computer Science Portal for Geeks" ; } function geek (x) { return "neveropen" ; } var gfg = _.mapArgsWith(geek); var gfgFunc = gfg(cs); console.log(gfgFunc()) |
Output:
neveropen : 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([ "neveropen" , "Computer Science Portal for Geeks" ])) |
Output:
neveropen : Computer Science Portal for Geeks