Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Converting String Arguments to Numbers:
4.2.
JavaScript
4.3.
Combining Objects from Separate Arguments:
4.4.
JavaScript
4.5.
Adjusting Date Formats:
4.6.
JavaScript
4.7.
Standardizing Case for String Inputs:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
Can _.overArgs() be used with functions that have varying numbers of arguments?
5.2.
How does _.overArgs() handle extra arguments?
5.3.
Is _.overArgs() applicable for all types of functions?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.overArgs() Method

Author Gaurav Gandhi
0 upvote

Introduction

Transforming arguments before they reach a function can significantly enhance flexibility and reusability in programming. Lodash steps in with the _.overArgs() method, a utility that creates a function that invokes the original function with arguments transformed by provided iteratees. This method is particularly useful when you need to preprocess arguments to fit a function's expected format or when dealing with legacy code where modifying the original function isn't feasible. 

Lodash _.overArgs() Method

This article will explore the _.overArgs() method, its syntax, use cases, and benefits, accompanied by examples and FAQs.

Why This Function is Used

The _.overArgs() function is used to modify or adapt arguments passed to a function. By applying transformation functions to each argument, it ensures that the main function receives inputs in the desired format or structure. This is particularly beneficial in scenarios where inputs come from varied sources or need to be standardized, normalized, or enriched before being processed by the main function.

Syntax, Parameter and Return Value

Syntax: 

_.overArgs(func, [transforms])

Parameters:

  • func (Function): The function to wrap.
     
  • [transforms] (Function[]): The argument transforms.

Return Value: 

(Function) - Returns the new function.

Examples 

Converting String Arguments to Numbers:

  • JavaScript

JavaScript

var _ = require('lodash');

function sum(a, b) {

 return a + b;

}

var sumWithConversion = _.overArgs(sum, [parseFloat, parseFloat]);

console.log(sumWithConversion('1.2', '3.4'));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

4.6


Demonstrates transforming string arguments to numbers before summing.

Combining Objects from Separate Arguments:

  • JavaScript

JavaScript

function combineObjects(a, b) {

 return { ...a, ...b };

}

var prepareAndCombine = _.overArgs(combineObjects, [JSON.parse, JSON.parse]);

console.log(prepareAndCombine('{"name": "Alice"}', '{"age": 30}'));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ name: 'Alice', age: 30 }


Shows parsing JSON strings before combining into a single object.

Adjusting Date Formats:

  • JavaScript

JavaScript

function scheduleEvent(date) {

 console.log('Event scheduled for:', date.toISOString());

}

var scheduleWithFormattedDate = _.overArgs(scheduleEvent, [Date.parse]);

scheduleWithFormattedDate('2021-01-01');
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'Event scheduled for: 2021-01-01T00:00:00.000Z'


An example of converting a date string to a Date object before processing.

Standardizing Case for String Inputs:

  • JavaScript

JavaScript

function greet(name) {

 console.log('Hello, ' + name);

}

var greetWithProperCase = _.overArgs(greet, [_.startCase]);

greetWithProperCase('alice');
You can also try this code with Online Javascript Compiler
Run Code

 Output: 

'Hello, Alice'


Demonstrates transforming a lowercase string to start case before greeting.

Frequently Asked Questions 

Can _.overArgs() be used with functions that have varying numbers of arguments?

Yes, _.overArgs() can be used with functions having different numbers of arguments. Each transformation function corresponds to an argument based on their order.

How does _.overArgs() handle extra arguments?

Extra arguments not matched with a transformation function are passed to the wrapped function in their original form.

Is _.overArgs() applicable for all types of functions?

_.overArgs() is most beneficial for functions where pre-processing or standardization of arguments is necessary. It may not be as useful for functions that accept standardized or pre-validated inputs.

Conclusion

Lodash's _.overArgs() method offers a versatile way to preprocess function arguments, enabling more flexible and adaptable code. By transforming arguments before they reach the function, it enhances the ability to integrate diverse data sources and formats seamlessly.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass