Friday, January 10, 2025
Google search engine
HomeLanguagesJavascriptLodash _.mapArgs() Method

Lodash _.mapArgs() Method

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

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