The Lodash _.mapArgs() method takes a target function and returns a new function that accepts a mapping function, which in turn returns a function that will map its arguments before calling the original target function.
Syntax:
_.mapArgs( target_function, mapping_function );
Parameters: This method accepts two parameters as mentioned above and defined below:
- target_function: This parameter is original function called.
- mapping_function: It is a 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 _.mapArgs() 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; } function cube (x) { return x * x * x; } var cubethenadd = _.mapArgs(add)(cube); console.log(cubethenadd(5)) |
Output:
250
Example 2:
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' ); function add (x) { return x + x; } function sub (x) { return x - x; } var subthenadd = _.mapArgs(add)(sub); console.log(subthenadd(7)) |
Output:
0