Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptLodash _.overArgs() Method

Lodash _.overArgs() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.overArgs() method is used to create a function that invokes func with its arguments transformed using the given transforms function.

Syntax:

_.overArgs(func, transforms )

Parameters: This method accepts two parameters as mentioned above and described below:

  • func: This parameter holds the function to wrap.
  • transforms: This parameter holds the function that specifies the argument transforms. It is an optional parameter.

Return Value: This method returns the new function.

Example 1:

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
 
// Function to calculate the
// Cube of a number
function Cube(number) {
  return number * number * number;
}
 
// Function to calculate the
// triple value of a number
function Triple(number) {
  return number * 3;
}
  
// Using the _.overArgs() method 
var func = _.overArgs(function(a, b) {
  return [a, b];
}, [Cube, Triple]);
 
// print the output
console.log(func(3, 5));


Output:

[27, 15]

Example 2:  

Javascript




// Function to calculate the
// double value of a number
function doubled(number) {
  return number * 2;
}
 
// Function to calculate the
// square value of a number
function square(number) {
  return number * number;
}
  
// Using the _.overArgs() method 
var func = _.overArgs(function(a, b) {
  return [a, b];
}, [square, doubled]);
 
// print the output
console.log(func(5, 8));


Output:

[25, 16]

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