Lodash _.dropRightWhile() method
What you’ll learn
- How
_.dropRightWhile(array, predicate)removes the longest trailing run where your predicate is truthy. - Using functions versus Lodash iteratee shorthand (such as a property name string).
- When to prefer
dropRightWhileover a fixeddropRight(n). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.dropRight() first; basic comfort with Array.prototype.filter helps contrast behavior.
- 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
_.dropRightWhile trims a suffix determined by your predicate, not a fixed length. That fits ragged trailing data: padding zeros, completed tasks at the end of a queue snapshot, or repeated sentinel values.
Non-destructive
The source array is never modified; you always get a fresh array reference.
Predicate-driven
Stop as soon as the scan hits a value that fails the test; the kept prefix can be any length.
End-first scan
Lodash walks from the last index backward, but returned order still reads left to right.
Syntax
_.dropRightWhile(array, [predicate=_.identity]) - array: source collection (array-like values are coerced).
- predicate: invoked as
(value, index, array); suffix 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 trailing suffix.
Repeated trailing values
Only the contiguous tail matching the test is removed; earlier matches inside the array are left alone.
import dropRightWhile from "lodash/dropRightWhile";
dropRightWhile([1, 2, 3, 2, 2], (x) => x === 2);
// [1, 2, 3] Strip trailing zeros
A common numeric cleanup: remove padding zeros from the right until a non-zero digit appears.
import dropRightWhile from "lodash/dropRightWhile";
dropRightWhile([1, 2, 3, 0, 0, 0], (x) => x === 0);
// [1, 2, 3] Property shorthand
Pass a string iteratee to test truthiness of a field on each object; the scan still runs from the end.
import dropRightWhile from "lodash/dropRightWhile";
dropRightWhile(
[
{ id: 1, closed: false },
{ id: 2, closed: true },
{ id: 3, closed: true }
],
"closed"
);
// [{ id: 1, closed: false }] 📋 dropRightWhile vs dropRight
| API | Trim rule | Best when |
|---|---|---|
_.dropRightWhile(arr, pred) | Variable-length suffix while predicate is truthy | Padding, sentinels, or heterogeneous tail you cannot count ahead of time |
_.dropRight(arr, n) | Fixed count from the end | You always know exactly how many elements to remove |
Pitfalls to avoid
Not the same as filter
filter can remove matching elements anywhere. dropRightWhile only removes a contiguous block at the end.
Side effects in predicates
Because evaluation walks from the end, relying on hidden mutable state can surprise you. Prefer pure predicates.
Leading trim
For a prefix controlled by a predicate, use _.dropWhile() or combine findIndex with slice.
❓ FAQ
Summary
- Purpose:
_.dropRightWhile(array, predicate)returns a new array without the longest trailing suffix where the predicate is truthy. - Safety: the input array is not mutated.
- Next: Lodash _.dropWhile(), _.dropRight(), or the array methods hub.
The predicate receives (value, index, array) where index is still the normal left-to-right index of that element—Lodash only changes which elements it visits first, not the index numbering.
6 people found this page helpful
