Lodash _.takeRightWhile() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates a slice of array with elements taken from the end. Elements are taken until predicate returns falsey. Part of Lodash Array since 3.0.0 (stable in 4.x — not deprecated).

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.takeRightWhile on an array. Start with this line.

Lodash
_.takeRightWhile(array, [predicate=_.identity])
CallWhat it does
_.takeRightWhile(array, [predicate=_.identity])Primary signature.
_.takeRightWhile(array)Call with an array argument.

Prefer import takeRightWhile from "lodash/takeRightWhile" when you want a smaller bundle.

Library declarations
Lodash
_.takeRightWhile(array, [predicate=_.identity])

Parameters

ParameterDescription
arrayRequired. Source to query
predicateOptional. Invoked as (value, index, array); default _.identity

Return Value

Return ValueDescription
*New array of the matching trailing suffix

Lodash Examples

Example 1 — Official: Function Predicate

Keep trailing inactive users until the first active one from the end.

Lodash
var users = [
  { user: "barney",  active: true },
  { user: "fred",    active: false },
  { user: "pebbles", active: false }
];

_.takeRightWhile(users, function (o) { return !o.active; });
// → [{ user: "fred", active: false }, { user: "pebbles", active: false }]
Try It Yourself

How It Works

  1. Source. Three users; last two are inactive.
  2. Predicate. !o.active is truthy for inactive users.
  3. Take suffix. pebbles and fred are kept from the end.
  4. Stop. barney is active → predicate falsy → stop; left out of the result.

Example 2 — Official: Matches Object

Keep while the element deeply matches a partial object.

Lodash
var users = [
  { user: "barney",  active: true },
  { user: "fred",    active: false },
  { user: "pebbles", active: false }
];

_.takeRightWhile(users, { user: "pebbles", active: false });
// → [{ user: "pebbles", active: false }]
Try It Yourself

How It Works

  1. Shorthand. An object uses Lodash _.matches iteratee rules.
  2. Last item. pebbles matches → kept.
  3. Next from end. fred does not match → stop.
  4. Result. Only pebbles remains.

Example 3 — Official: MatchesProperty Pair

Keep while active === false using a [path, value] pair.

Lodash
var users = [
  { user: "barney",  active: true },
  { user: "fred",    active: false },
  { user: "pebbles", active: false }
];

_.takeRightWhile(users, ["active", false]);
// → [{ user: "fred", active: false }, { user: "pebbles", active: false }]
Try It Yourself

How It Works

  1. Pair form. ["active", false] means “property equals value”.
  2. Take. Both trailing inactive users match and are kept.
  3. Stop. barney.active is true → not equal → stop.
  4. Keep. The inactive suffix is returned in order.

Example 4 — Official: Property Shorthand

A string tests truthiness — falsy at the end means nothing is taken.

Lodash
var users = [
  { user: "barney",  active: true },
  { user: "fred",    active: false },
  { user: "pebbles", active: false }
];

_.takeRightWhile(users, "active");
// → []
Try It Yourself

How It Works

  1. Property iteratee. "active" reads each object’s active field.
  2. Last value. pebbles.active is false (falsy).
  3. Stop at once. Falsy ends the take — result is empty.
  4. Tip. Use this when trailing items are truthy and you want that run.

Example 5 — Trailing Zeros

Only the contiguous trailing run is kept; an earlier zero is left out.

Lodash
_.takeRightWhile([0, 1, 2, 0, 0], function (x) {
  return x === 0;
});
// → [0, 0]
Try It Yourself

How It Works

  1. Trailing run. Last two values equal 0 → kept.
  2. Break. 2 fails x === 0 → stop.
  3. Prefix left out. Leading 0 is not in the result.
  4. Rule. Unlike filter, earlier matches before the break are not kept.

Browser & Runtime Support

_.takeRightWhile is a Lodash 4.x Array helper (since 3.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 3.0.0+

_.takeRightWhile()

Available in [email protected] full builds and the lodash/takeRightWhile modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.takeRightWhile Lodash 3.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import takeRightWhile from "lodash/takeRightWhile" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.takeRightWhile(array, predicate) walks from the end and keeps elements while the predicate returns a truthy value. At the first falsy result it stops and returns a new array of that trailing suffix (in original order).
No. It returns a new array slice. The source array is left unchanged. Object elements inside are still shared by reference.
filter can keep matches anywhere. takeRightWhile only keeps a contiguous suffix from the end — once the predicate fails, earlier elements are not included, even if they would match.
You get a shallow copy of the whole array. If the last element already fails the predicate, you get [].
Use import takeRightWhile from "lodash/takeRightWhile" or require("lodash/takeRightWhile") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.takeRightWhile.
Same as other Lodash iteratees: a function, a matches object, a matchesProperty pair like ["active", false], or a property name string. Default predicate is _.identity.

Next: _.takeWhile()

Keep a leading prefix with a predicate — the mirror of _.takeRightWhile.

_.takeWhile() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful