Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
Collecting Additional Arguments:
4.2.
JavaScript
4.3.
Creating Variadic Functions:
4.4.
JavaScript
4.5.
Handling Event Listeners:
4.6.
JavaScript
4.7.
Processing Command-Line Arguments:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How is _.rest() different from native JavaScript rest parameters?
5.2.
Can _.rest() be used with functions of any arity?
5.3.
Is the original function modified by _.rest()?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.rest() Method

Author Pallavi singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In programming, especially functional programming, manipulating the arguments passed to functions can be crucial. Lodash’s _.rest() method is designed to enhance function argument handling by creating a new function that invokes the provided function with any additional arguments after the first n arguments. 

Lodash _.rest() Method

This is particularly useful for functions that need to handle an unspecified number of arguments, or for simplifying functions that take arrays of arguments.

Why This Function is Used

The _.rest() function is used to simplify handling of variadic functions - functions that can take a variable number of arguments. By converting the additional arguments into an array, it makes it easier to process them as a group. This method is beneficial when you have functions that need to operate on the rest of the arguments, regardless of how many there are.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax:

_.rest(func, [start=func.length-1])

Parameters:

  • func (Function): The function to apply the rest parameter to.
     
  • [start=func.length-1] (number): The position at which to start collecting arguments.

Return Value: 

(Function) - Returns the new function.

Examples 

Collecting Additional Arguments:

  • JavaScript

JavaScript

var _ = require('lodash');

function assembleMessage(firstWord, ...restWords) {

 return firstWord + ' ' + restWords.join(' ');

}

var enhancedAssembleMessage = _.rest(assembleMessage);

console.log(enhancedAssembleMessage('Hello', 'world', 'from', 'Lodash!'));

Output

'Hello world from Lodash!'


Demonstrates collecting all arguments beyond the first into an array.

Creating Variadic Functions:

  • JavaScript

JavaScript

function sum(...numbers) {

 return numbers.reduce((total, n) => total + n, 0);

}

var variadicSum = _.rest(sum, 2);

console.log(variadicSum(1, 2, 3, 4));

Output: 

9 (sum of 2, 3, 4)


Shows creating a function that sums all arguments after the first two.

Handling Event Listeners:

  • JavaScript

JavaScript

function handleEvent(eventType, ...details) {

 console.log(`Event: ${eventType}, Details:`, details);

}

var enhancedHandleEvent = _.rest(handleEvent);

enhancedHandleEvent('click', 'button1', 'header', 'footer');function handleEvent(eventType, ...details) {

 console.log(`Event: ${eventType}, Details:`, details);

}

var enhancedHandleEvent = _.rest(handleEvent);

enhancedHandleEvent('click', 'button1', 'header', 'footer');

Output: 

'Event: click, Details: ['button1', 'header', 'footer']'


An example of using _.rest() to handle variadic arguments in event listeners.

Processing Command-Line Arguments:

  • JavaScript

JavaScript

function processCommands(command, ...options) {

 console.log(`Command: ${command}, Options:`, options);

}

var processCommandsVariadic = _.rest(processCommands);

processCommandsVariadic('start', '--verbose', '--timeout=20');

Output

'Command: start, Options: ['--verbose', '--timeout=20']'


Demonstrates processing a list of command-line arguments.

Frequently Asked Questions

How is _.rest() different from native JavaScript rest parameters?

_.rest() is similar to native JavaScript rest parameters but offers more control, allowing you to specify from which argument to start collecting the rest of the arguments.

Can _.rest() be used with functions of any arity?

Yes, _.rest() can be used with functions of any arity. It's particularly effective with functions where the number of arguments isn't fixed.

Is the original function modified by _.rest()?

No, _.rest() creates a new function and does not modify the original function. The original function remains as it is.

Conclusion

Lodash's _.rest() method offers a convenient way to handle variadic functions, allowing for the easy grouping of arguments beyond a certain point. It's a useful tool for functions that need to process an indefinite number of arguments or need to operate on the latter part of their arguments.

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.

Previous article
Lodash _.map() Method
Next article
Lodash _.reduceRight() Method
Live masterclass