The Lodash _.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: This method accepts a single parameter as mentioned above and defined below:
- mapping_function: Mapping function to be accepted by the function.
Return Value: This method returns a function.
Note: To execute the below examples, you have to install the lodash-contrib library by using this command prompt and execute the following command.
npm install lodash-contrib
Below examples illustrate the Lodash _.mapArgsWith() method in JavaScript:
Example 1: we made a function that cubes the given value then adds that value to itself.
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-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(7)) |
Output:
15
Example 2:
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-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 lodash contrib variable var _ = require( 'lodash-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 lodash contrib variable var _ = require( 'lodash-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