javascript

Lodash - Get all elements of array except the last one

To get all elements from an array except the last one or to remove last element from the array using lodash, you can use _.initial() method of lodash

const arr = [10, 20, 30, 40, 50];

const result = _.initial(arr);
// => [10,20,30,40]

In the code snippet,  we have an array arr and we want to get all elements of it except the last one. We are doing that using the .initial() method of lodash.

_.initial Lodash

_.initial(Array)

Parameters description

Array: [Required] The array whose elements you want to get except the last one.

Was this helpful?