Syntax, Parameter, and Return Value
Syntax:
_.dropWhile(array, [predicate=_.identity])
Parameters:
-
array (Array): The array to inspect.
- predicate (Function): The function invoked per iteration.
Return Value:
Returns the slice of array.
Examples with Proper Codes
Example 1: Using a Function Predicate
JavaScript
const _ = require('lodash');
const users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': true },
{ 'user': 'pebbles', 'active': true }
];
const result = _.dropWhile(users, function(o) { return !o.active; });
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'fred', 'active': true }, { 'user': 'pebbles', 'active': true }]
Drops elements from the start as long as they are not active.
Example 2: Using an Arrow Function
JavaScript
const numbers = [1, 2, 3, 4, 5, 6];
const result = _.dropWhile(numbers, n => n < 3);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[3, 4, 5, 6]
Drops numbers less than 3 from the start of the array.
Example 3: Property Shorthand
JavaScript
const users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
const result = _.dropWhile(users, {'user': 'barney', 'active': false});
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }]
Uses an object to specify the predicate.
Example 4: Property Name Shorthand
JavaScript
const users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
const result = _.dropWhile(users, 'active');
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }]
Drops users from the start as long as the 'active' property is false.
Frequently Asked Questions
How does _.dropWhile() ensure only the correct elements are dropped?
It iterates over the array until the predicate returns false for the first time, ensuring only the intended elements are dropped.
Can _.dropWhile() be used with arrays of primitive values?
Yes, it works with any array, whether it contains objects, numbers, strings, or other primitives.
Is _.dropWhile() efficient for large arrays?
Yes, it is optimized for performance, especially when dealing with large arrays or complex predicates.
Conclusion
The _.dropWhile() method in Lodash is a sophisticated tool for array manipulation, offering conditional removal of elements from the start of an array. This method enhances the flexibility of array handling, allowing developers to specify detailed conditions for trimming arrays. Whether dealing with data streams or preparing datasets, _.dropWhile() stands out as a crucial function for efficient and precise array processing in JavaScript.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.