javascript
Remove first n elements from an array using Lodash
You can use _.drop method of lodash to remove first n elements from an array. To remove first 3 elements from an array pass n value as 3.
const arr = [1, 2, 3, 4, 5, 6, 7];
//remove first element (n=1) by default
const arr1 = _.drop(arr);
//remove first 3 elements
const arr2 = _.drop(arr, 3);
//remove first 5 elements
const arr3 = _.drop(arr, 5);
Output
[2,3,4,5,6,7]
[4,5,6,7]
[6,7]
[4,5,6,7]
[6,7]
We can easily remove first n items from an array using _.drop method if we are using Lodash javascript library in our project. The syntax and description of _.drop is as below.
_.drop lodash
Basic syntax of _.drop is as below which can be used to drop first n items from given array.
_.drop(Array, n=1)
Array: This argument is of array type and this contains the values that you want to remove from this array. This is a required parameter.
n: Here we pass an integer type value. This is an options argument. The default value of n is 1 means if you do not pass any value it will remove very first element from the array.
Example Demo for _.drop
Was this helpful?
Similar Posts
- Remove last n elements from an array using Lodash
- Lodash - Get all elements of array except the last one
- Lodash - Create a string from array elements join with some separator
- Lodash - get the first element from an array
- Lodash - Get index of first occurence of an array value
- How to Remove duplicate elements from array in JavaScript ?
- Join two arrays and remove duplicate elements using Javascript