How to use the rearg function from lodash

Find comprehensive JavaScript lodash.rearg code examples handpicked from public code repositorys.

lodash.rearg is a function in the lodash library that creates a new function with arguments rearranged according to a specified array of indexes.

314
315
316
317
318
319
320
321
322
323
module.exports.random              = _.random;
module.exports.range               = _.range;
module.exports.rangeRight          = _.rangeRight;
module.exports.rcurry2             = _.rcurry2;
module.exports.rcurry3             = _.rcurry3;
module.exports.rearg               = _.rearg;
module.exports.reduce              = _.reduce;
module.exports.reduceRight         = _.reduceRight;
module.exports.reductions          = _.reductions;
module.exports.reject              = _.reject;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

341
342
343
344
345
346
347
348
349
350
351
352
353
console.log(partial(3)); // => 6


const partialRight = _.partialRight((a, b, c) => a + b + c, 1, 2);
console.log(partialRight(3)); // => 6


const rearg = _.rearg((a, b, c) => [a, b, c], [2, 0, 1]);
console.log(rearg(1, 2, 3)); // => [3, 1, 2]


const rest = _.rest((a, b, ...c) => [a, b, c]);
console.log(rest(1, 2, 3, 4)); // => [1, 2, [3, 4]]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.rearg work?

lodash.rearg is a higher-order function in the lodash library that takes a function as its first argument and an array of indexes as its second argument. It returns a new function that has its arguments rearranged according to the specified indexes. The rearg function is useful when you need to change the order of arguments to a function but do not want to modify its original implementation. The new function returned by rearg can be used as a replacement for the original function, with the arguments reordered as needed. Here's an example of how you might use lodash.rearg to create a new function with rearranged arguments: javascript Copy code {{{{{{{ const _ = require('lodash'); function greet(firstName, lastName) { console.log(`Hello, ${firstName} ${lastName}!`); } const greetRearranged = _.rearg(greet, [1, 0]); greetRearranged('John', 'Doe'); // Output: "Hello, Doe John!" In this example, we start by importing the lodash library. We then define a function called greet that takes two arguments: firstName and lastName. The function logs a greeting to the console using the arguments. We then use _.rearg to create a new function called greetRearranged with the same implementation as greet, but with its arguments rearranged so that the last name is logged before the first name. We pass greet as the first argument to _.rearg, and an array of indexes [1, 0] as the second argument to indicate that we want to swap the positions of the arguments. Finally, we call greetRearranged with 'John' and 'Doe' as its arguments. The function logs the greeting "Hello, Doe John!" to the console, which shows that the arguments have been rearranged as expected. Note that lodash.rearg is useful for tasks such as adapting functions to work with APIs that expect arguments in a different order. It can also be used with other lodash functions such as _.flow or _.partial to compose new functions with rearranged arguments.

219
220
221
222
223
224
225
226
227
228
229
var sayHelloTo3 = sayHelloTo2('fred');
console.log('sayHelloTo3--->', sayHelloTo3);
//sayHelloTo3---> hello fred


//_.rearg(func,indexes)
var rearged = _.rearg(function (a, b, c) {
    return [a, b, c];
}, [2, 0, 1]);
let rearged1 = rearged('b', 'c', 'a');
console.log('rearged1--->', rearged1);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("lodash");

function greet(firstName, lastName) {
  console.log(`Hello, ${firstName} ${lastName}!`);
}

const greetRearranged = _.rearg(greet, [1, 0]);

greetRearranged("John", "Doe"); // Output: "Hello, Doe John!"

In this example, we define a function called greet that takes two arguments: firstName and lastName. The function logs a greeting to the console using the arguments. We then use _.rearg to create a new function called greetRearranged with the same implementation as greet, but with its arguments rearranged so that the last name is logged before the first name. We pass greet as the first argument to _.rearg, and an array of indexes [1, 0] as the second argument to indicate that we want to swap the positions of the arguments. Finally, we call greetRearranged with 'John' and 'Doe' as its arguments. The function logs the greeting "Hello, Doe John!" to the console, which shows that the arguments have been rearranged as expected.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)