Last Updated : 30 Aug, 2024
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:
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4