What is the _.dropRight() method in Lodash?

Overview

The _.dropRight() method in Lodash creates a slice of an array by dropping the n number of elements from the end of the array.

Syntax

_.dropRight(array, n)

Parameters

This method accepts the following parameters:

  • array: This is the array to be sliced.
  • n: This is the number of the elements to be dropped.

Return value

This method returns a slice of an array.

Example

Let’s look at an example of the _.dropRight() method in the code snippet below:

Console
Implementation of the _.dropRight() method

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create an array and initialize it with a few values.
  • Line 5: We create a variable n and initialize it with three.
  • Line 8: We use the _.drop() method to drop n elements from the end of the array.
  • Line 11: We print the output to the console.

Output

We see a slice with n number of elements dropped from the end of the array.

Free Resources