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
var _ = require('lodash');
function sum(a, b) {
return a + b;
}
var sumWithConversion = _.overArgs(sum, [parseFloat, parseFloat]);
console.log(sumWithConversion('1.2', '3.4'));
![](https://files.codingninjas.in/fluent_code-24-filled-29586.svg)
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
function combineObjects(a, b) {
return { ...a, ...b };
}
var prepareAndCombine = _.overArgs(combineObjects, [JSON.parse, JSON.parse]);
console.log(prepareAndCombine('{"name": "Alice"}', '{"age": 30}'));
![](https://files.codingninjas.in/fluent_code-24-filled-29586.svg)
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
function scheduleEvent(date) {
console.log('Event scheduled for:', date.toISOString());
}
var scheduleWithFormattedDate = _.overArgs(scheduleEvent, [Date.parse]);
scheduleWithFormattedDate('2021-01-01');
![](https://files.codingninjas.in/fluent_code-24-filled-29586.svg)
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
function greet(name) {
console.log('Hello, ' + name);
}
var greetWithProperCase = _.overArgs(greet, [_.startCase]);
greetWithProperCase('alice');
![](https://files.codingninjas.in/fluent_code-24-filled-29586.svg)
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.