Open In App
Related Articles

Lodash _.rearg() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Lodash _.rearg() method in lodash is used to create a function that calls the func parameter with the arguments that are organized according to the stated indexes. Where the argument valued at the first index is sent as the first argument, the argument valued at the second index is sent as the second argument, and so on.

Syntax:

_.rearg(func, indexes);

Parameters:

  • func: It is the function that is used to reorganize the arguments.
  • indexes: It is the indexes of organized argument.

Return Value:

This method returns the new function.

Example 1: In this example, we arrange the given array according to the given sequence and print the result in the console.

Javascript

// Requiring lodash library
const _ = require('lodash');
 
// Calling rearg() method with its parameter
let fn = _.rearg(function (x, y, z) {
    return [x, y, z];
}, [2, 1, 0]);
 
// Calling fn
fn('z', 'y', 'x');

                    

Output:

[ 'x', 'y', 'z' ]

Example 2: In this example, we arrange the given array according to the given sequence and print the result in the console.

Javascript

// Requiring lodash library
const _ = require('lodash');
 
// Calling rearg() method with its parameter
let concat = _.rearg(function (Geeks, forGeeks) {
    return (Geeks + forGeeks);
}, [1, 0]);
 
// Calling concat
concat('forGeeks', 'Geeks');

                    

Output:

GeeksforGeeks

Reference: https://lodash.com/docs/4.17.15#rearg



Last Updated : 03 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads