Lodash _.dropWhile() method
What you’ll learn
- How
_.dropWhile(array, predicate)removes the longest leading run where your predicate is truthy. - Using functions versus Lodash iteratee shorthand (such as a property name string).
- How
dropWhilepairs with _.dropRightWhile() and fixed-count_.drop. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.drop() and _.dropRightWhile() on this site for context.
- You can write a pure predicate
(value, index, array) => booleanwithout mutating arguments. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.dropWhile trims a prefix defined by your predicate—ideal for skipping leading padding, blank header rows, or tokens until real data begins. It is the mirror image of _.dropRightWhile, which trims from the opposite end.
Non-destructive
The source array stays the same; you always receive a new array reference.
Left-to-right scan
Evaluation starts at index 0 and stops on the first element that fails the predicate.
Predicate-driven
Unlike drop(n), you do not need to know the prefix length in advance.
Syntax
_.dropWhile(array, [predicate=_.identity]) - array: source collection (array-like values are coerced).
- predicate: invoked as
(value, index, array); prefix elements are dropped while the result is truthy. Strings can select a property path like other Lodash iteratees. - Returns: a new array without the longest matching leading prefix.
Leading zeros
Only the contiguous leading run is removed; zeros later in the array stay put.
import dropWhile from "lodash/dropWhile";
dropWhile([0, 0, 1, 2, 0], (x) => x === 0);
// [1, 2, 0] While a condition holds
Drop leading even numbers until an odd value breaks the predicate.
import dropWhile from "lodash/dropWhile";
dropWhile([2, 4, 6, 1, 3], (x) => x % 2 === 0);
// [1, 3] Property shorthand
A string iteratee tests truthiness of a field; leading objects with a truthy skip are removed.
import dropWhile from "lodash/dropWhile";
dropWhile(
[
{ id: 1, skip: true },
{ id: 2, skip: true },
{ id: 3, skip: false }
],
"skip"
);
// [{ id: 3, skip: false }] 📋 dropWhile vs drop vs dropRightWhile
| API | Trim rule | Direction |
|---|---|---|
_.dropWhile(arr, pred) | Leading run while predicate is truthy | Start of the array |
_.drop(arr, n) | Fixed n elements from the start | Start of the array |
_.dropRightWhile(arr, pred) | Trailing run while predicate is truthy | End of the array |
Pitfalls to avoid
Not the same as filter
filter can remove matches anywhere. dropWhile only removes a contiguous block at the beginning.
Trailing trim
For suffix predicates, use _.dropRightWhile() instead.
Iteratee shorthand
Property strings use truthiness of the resolved value; 0, "", and false count as falsy and stop the prefix.
❓ FAQ
Summary
- Purpose:
_.dropWhile(array, predicate)returns a new array without the longest leading prefix where the predicate is truthy. - Safety: the input array is not mutated.
- Next: Lodash _.fill(), _.dropRightWhile(), or the array methods hub.
Lodash walks indices 0, 1, 2, … in order and stops at the first element for which the predicate returns a falsy value; everything from that index onward is kept unchanged.
6 people found this page helpful
