Lodash _.takeRightWhile() method
What you’ll learn
- How
_.takeRightWhile(array, predicate)isolates a predicate-stable suffix. - Why only the contiguous tail qualifies—earlier matches stay behind.
- How this differs from fixed counts (_.takeRight()) and global filters.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.dropRightWhile() for the complementary operation—it removes the suffix takeRightWhile would keep.
- You can read arrow predicates and Lodash iteratee shorthand (
"prop", paths). - You can open Try-it labs or run snippets locally.
Overview
_.takeRightWhile shines when trailing noise shares a pattern—parity checks, sentinel flags, nullable padding—but you cannot hard-code how many cells participate.
End-first scan
The predicate walks inward until it falters; everything after that pivot stays excluded.
Stable ordering
Winners remain sorted left-to-right relative to each other even though evaluation reversed direction.
Fresh array
Source buffers remain untouched for auditing or alternate predicates.
Syntax
_.takeRightWhile(array, [predicate=_.identity]) - array: collection or array-like input.
- predicate: invoked per element as
(value, index, array); trailing cells join the result while truthy. Supports iteratee shorthand such as property strings. - Returns: new array holding only that contiguous trailing suffix.
Trailing run of even numbers
Scanning from the right: 14, 12, and 10 pass; 5 stops the suffix.
import takeRightWhile from "lodash/takeRightWhile";
takeRightWhile([5, 10, 12, 14], (n) => n % 2 === 0);
// → [10, 12, 14] Immediate miss
When the final element already fails the predicate, nothing is collected.
import takeRightWhile from "lodash/takeRightWhile";
takeRightWhile([1, 3, 5], (n) => n % 2 === 0);
// → [] Property shorthand
String iteratees test truthiness of a field—here only objects still marked closed survive at the tail.
import takeRightWhile from "lodash/takeRightWhile";
takeRightWhile(
[
{ id: 1, closed: false },
{ id: 2, closed: true },
{ id: 3, closed: true }
],
"closed"
);
// → [{ id: 2, closed: true }, { id: 3, closed: true }] 📋 takeRightWhile vs takeRight, dropRightWhile, filter
| API | Selection rule | Contrast |
|---|---|---|
_.takeRightWhile(arr, pred) | Longest truthy suffix | Returns only that tail segment |
_.takeRight(arr, n) | Fixed count | No predicate—length known up front |
_.dropRightWhile(arr, pred) | Removes truthy suffix | Keeps complementary prefix instead |
_.filter(arr, pred) | Every matching element | Not limited to one contiguous edge |
Pitfalls to avoid
Not global
Interior matches never surface—only the block touching the final index qualifies.
Predicate hygiene
Reverse evaluation order makes sneaky mutable state harder to reason about—keep predicates pure.
Shallow copies
Objects inside the suffix remain referenced—clone deeply when isolation matters.
❓ FAQ
Summary
- Purpose:
_.takeRightWhile(array, predicate)clones the longest trailing run where the iteratee stays truthy. - Inverse: pair with _.dropRightWhile() when you need the complementary head.
- Next: Lodash _.takeWhile(), _.takeRight() (previous), or the array methods hub.
The iteratee still receives (value, index, array) with ordinary left-to-right index values—only the visit order starts from the end. Pair mentally with _.dropRightWhile(): one keeps the tail you trim with the other.
6 people found this page helpful
