The Wayback Machine - https://web.archive.org/web/20240204103815/https://www.geeksforgeeks.org/lodash-_-dropwhile-method/
Open In App
Related Articles

Lodash _.dropWhile() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers etc.
The Loadsh.dropWhile() method is used to return the slice of the given array. This takes a predicate function that iterate through each element of the array and if the function returns false, it returned the sliced array excluding elements dropped from the beginning.

Syntax:

dropWhile(array, [predicate=_.identity])

Parameters:

  • array: It is the array that is to be sliced.
  • predicate: It is the function that either returns true or false depending on the condition given.

Return Value: It returns the new array after slicing.

Note: Please install lodash module by using command npm install lodash before using the code given below.

Example 1: 

Javascript




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [1, 3, 4, 5, 5, 6]
  
// Using _.dropWhile() method
let newArray = _.dropWhile(array1, (e) => {
    return e != 5;
});
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the newArray
console.log("new Array: ", newArray)


Output: 

Example 2: When array of object is given.

Javascript




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [
    { "a": 1, "b": 2 }, 
    { "a": 2, "b": 1 }, 
    { "b": 2 }
]
  
// Using _.dropWhile() method
let newArray = _.dropWhile(array1, (e) => {
    return e.b == 2;
});
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the newArray
console.log("new Array: ", newArray)


Output: 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 29 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials