Thursday, December 11, 2025
HomeLanguagesJavascriptLodash _.dropRight() Function

Lodash _.dropRight() Function

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.dropRight() function is used to delete the elements from the right of the array i.e from the (n-1)th element.

Syntax: 

_.dropRight(array, n)

Parameter:

  • array: It is the original array from which elements are to be deleted.
  • n: Here n is the number of elements that are to be deleted from the array. By default, it is set to one.

Return Value: It returns the array.

Note: Install the lodash module by using command npm install lodash before using the code given below.

Example 1: When n is less than the size of an array.




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [1, 2, 3, 4, 5]
  
// Using _.dropRight() function
let newArray = lodash.dropRight(array1, 2);
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the newArray
console.log("new Array: ", newArray)


Output: 

Example 2: When n is greater than the size of the array.




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [1, 2, 3, 4, 5]
  
// Using _.dropRight() function
let newArray = lodash.dropRight(array1, 10);
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the newArray
console.log("new Array: ", newArray)


Output: 

Example 3: When an array of objects is given and n is not given.




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [
    { "a": 1, "b": 2 }, 
    { "a": 2, "b": 1 }, 
    { "b": 2 }
]
  
// Using _.dropRight() function
let newArray = lodash.dropRight(array1);
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the newArray
console.log("new Array: ", newArray)


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32440 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6811 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12023 POSTS0 COMMENTS
Shaida Kate Naidoo
6942 POSTS0 COMMENTS
Ted Musemwa
7193 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS