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 with Proper Codes
4.1.
Example 1: Basic Usage
4.2.
JavaScript
4.3.
Example 2: Default Behavior
4.4.
JavaScript
4.5.
Example 3: Dropping No Elements
4.6.
JavaScript
4.7.
Example 4: Non-Integer Values
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
What if n exceeds the array's length in _.dropRight()?
5.2.
Does _.dropRight() affect the original array?
5.3.
Can _.dropRight() be used with chain syntax in Lodash?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.dropRight() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

The _.dropRight() method in Lodash is a counterpart to the _.drop() method, designed for trimming arrays from the end. This function creates a slice of an array with n elements dropped from the end, making it a vital tool for developers when only a specific portion of an array is required, or when tail elements are no longer needed.

Lodash _.dropRight() Method

This method offers a clean and efficient way to handle array modifications without altering the original array structure.

Why This Function is Used

_.dropRight() is used for removing elements from the end of an array. It's particularly useful in situations where the tail elements of an array are either irrelevant, redundant, or already processed. Whether it's for data processing, manipulation, or simply preparing data for display, _.dropRight() provides a straightforward solution for tail-end array trimming.

Syntax, Parameter, and Return Value

Syntax:

_.dropRight(array, [n=1])

Parameters:

  • array (Array): The array to process.
     
  • n (number): The number of elements to drop from the end. Defaults to 1 if not specified.

Return Value:

Returns the slice of array.

Examples with Proper Codes

Example 1: Basic Usage

  • JavaScript

JavaScript

const _ = require('lodash');

const array = [1, 2, 3, 4, 5];

const result = _.dropRight(array, 2);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 [1, 2, 3]

Dropping the last two elements of the array.

Example 2: Default Behavior

  • JavaScript

JavaScript

const array = ['a', 'b', 'c'];

const result = _.dropRight(array);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

['a', 'b']

When no number is specified, _.dropRight() defaults to dropping the last element.

Example 3: Dropping No Elements

  • JavaScript

JavaScript

const array = [1, 2, 3];

const result = _.dropRight(array, 0);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

Output:

[1, 2, 3]


Specifying 0 will not drop any elements from the end.

Example 4: Non-Integer Values

  • JavaScript

JavaScript

const array = ['apple', 'banana', 'cherry'];

const result = _.dropRight(array, 1.5);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 ['apple', 'banana']


Non-integer values are floored, resulting in the dropping of the last element in this case.

Frequently Asked Questions

What if n exceeds the array's length in _.dropRight()?

If n is greater than the array's length, the function returns an empty array, as all elements are dropped from the end.

Does _.dropRight() affect the original array?

No, _.dropRight() returns a new array and does not modify the original array.

Can _.dropRight() be used with chain syntax in Lodash?

Yes, it can be chained with other Lodash methods for more complex data manipulation sequences.

Conclusion

The _.dropRight() method in Lodash is an efficient solution for trimming arrays from the end. Its ease of use and flexibility make it a key function in various array manipulation tasks in JavaScript. Whether it's processing large datasets or making quick modifications to data, _.dropRight() offers a practical approach to managing array elements.

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.

Live masterclass