Lodash _.dropRight() Function
Lodash _.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);
Parameters:
- 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: In this example, we are printing the new array having two elements less from the right side of the original array.
javascript
// Requiring the lodash library const _ = require( 'lodash' ); // Original array let array1 = [1, 2, 3, 4, 5] // Using _.dropRight() function let newArray = _.dropRight(array1, 2); // Original Array console.log( 'original Array: ' , array1) // Printing the newArray console.log( 'new Array: ' , newArray) |
Output:
Example 2: In this example, given value is greater than the length of the given array so it is giving empty array.
javascript
// Requiring the lodash library const _ = require( 'lodash' ); // Original array let array1 = [1, 2, 3, 4, 5] // Using _.dropRight() function let newArray = _.dropRight(array1, 10); // Original Array console.log( 'original Array: ' , array1) // Printing the newArray console.log( 'new Array: ' , newArray) |
Output:
Example 3: In this example, we are printing the new array having one element less from the right side of the original array.
javascript
// 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 = _.dropRight(array1); // Original Array console.log( 'original Array: ' , array1) // Printing the newArray console.log( 'new Array: ' , newArray) |
Output:
Contact Us