Open In App

How to use the drop method in Lodash ?

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Drop method in Lodash removes elements from the beginning of arrays or characters from the start of strings. You can implement the lodash drop method as implemented in the below approaches.

Run the below command before running the below code on your system:

npm i lodash

Removing elements from the beginning of an array using drop

In this approach, we are using Lodash’s drop method on the array to remove the first 2 elements from it, generating res as the modified array with those elements dropped, which is then logged to the console.

Syntax:

_.drop(array, numberOfElementsToBeRemoved)

Example: The below example uses the drop method to drop elements from the beginning of the array.

JavaScript
const _ = require('lodash');
const array = [1, 2, 3, 4, 5];
const res = _.drop(array, 2);
console.log(res);

Output:

[ 3, 4, 5 ]

Removing string characters from the beginning using drop

In this approach, we are using Lodash’s drop method on the string str by first splitting it into an array of characters, then dropping the first 3 characters from it, and finally joining the remaining characters back into a string stored in res, which is then logged to the console.

Syntax:

_.drop(str.split(''), numberOfElementsToBeRemoved).join('')

Example: The below example uses the drop method to drop elements from the beginning of the string.

JavaScript
const _ = require('lodash');
const str = 'Hello, GFG!';
const res = _.drop(str, 3).join('');
console.log(res);

Output:

lo, GFG!

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads