Syntax, Parameter and Return Value
Syntax:
_.rearg(func, indexes)
Parameters:
-
func (Function): The function to rearrange arguments for.
- indexes (Array|number): The arranged indexes of arguments.
Return Value:
(Function) - Returns the new function with rearranged argument order.
Examples
Swapping Arguments of a Function:
JavaScript
var _ = require('lodash');
function divide(a, b) {
return a / b;
}
var flippedDivide = _.rearg(divide, [1, 0]);
console.log(flippedDivide(2, 10));

You can also try this code with Online Javascript Compiler
Run Code
Output:
5 (10 / 2)
Demonstrates swapping the order of arguments for a division function.
Reordering Arguments for Compatibility:
JavaScript
function log(level, message, date) {
console.log(`[${date}] [${level}]: ${message}`);
}
var reorderedLog = _.rearg(log, [2, 0, 1]);
reorderedLog('INFO', 'Log message', new Date().toISOString());

You can also try this code with Online Javascript Compiler
Run Code
// Rearranges to put the date first, followed by level and message
Shows reordering arguments to fit a specific logging format.
Adjusting Argument Order for Event Handlers:
JavaScript
function customEventHandler(arg1, event) {
console.log(`Event type: ${event.type}, Data: ${arg1}`);
}
var eventHandler = _.rearg(customEventHandler, [1, 0]);
document.addEventListener('click', event => eventHandler('Click data', event));

You can also try this code with Online Javascript Compiler
Run Code
// Properly orders arguments for the event handler
An example of reordering arguments for an event handler function.
Function Adaptation for Array Methods:
JavaScript
function startsWith(prefix, string) {
return string.startsWith(prefix);
}
var checkStartsWith = _.rearg(startsWith, [1, 0]);
var words = ['apple', 'banana', 'avocado'];
var aWords = _.filter(words, _.partial(checkStartsWith, 'a'));
console.log(aWords);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['apple', 'avocado']
Demonstrates reordering a predicate function for use with _.filter.
Frequently Asked Questions
Can _.rearg() handle functions with multiple arguments?
Yes, _.rearg() is designed to handle functions with multiple arguments, allowing you to reorder them as needed.
How does _.rearg() differ from simply wrapping a function?
While you can wrap a function to reorder arguments, _.rearg() provides a more concise and declarative way to achieve this, especially for simple reordering needs.
Is the original function modified by _.rearg()?
No, _.rearg() creates a new function without altering the original function. The original function remains available with its original argument order.
Conclusion
Lodash's _.rearg() method is a practical tool for rearranging the order of arguments passed to a function. It offers a flexible way to adapt and interface with existing functions, enhancing compatibility and easing integration with different codebases or APIs.
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.