Lodash _.dropRightWhile() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 dropRightWhile over a fixed dropRight(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) => boolean without 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

javascript
_.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.
1

Repeated trailing values

Only the contiguous tail matching the test is removed; earlier matches inside the array are left alone.

javascript
import dropRightWhile from "lodash/dropRightWhile";

dropRightWhile([1, 2, 3, 2, 2], (x) => x === 2);
// [1, 2, 3]
Try it Yourself
2

Strip trailing zeros

A common numeric cleanup: remove padding zeros from the right until a non-zero digit appears.

javascript
import dropRightWhile from "lodash/dropRightWhile";

dropRightWhile([1, 2, 3, 0, 0, 0], (x) => x === 0);
// [1, 2, 3]
Try it Yourself
3

Property shorthand

Pass a string iteratee to test truthiness of a field on each object; the scan still runs from the end.

javascript
import dropRightWhile from "lodash/dropRightWhile";

dropRightWhile(
  [
    { id: 1, closed: false },
    { id: 2, closed: true },
    { id: 3, closed: true }
  ],
  "closed"
);
// [{ id: 1, closed: false }]
Try it Yourself

📋 dropRightWhile vs dropRight

APITrim ruleBest when
_.dropRightWhile(arr, pred)Variable-length suffix while predicate is truthyPadding, sentinels, or heterogeneous tail you cannot count ahead of time
_.dropRight(arr, n)Fixed count from the endYou always know exactly how many elements to remove

Pitfalls to avoid

Suffix

Not the same as filter

filter can remove matching elements anywhere. dropRightWhile only removes a contiguous block at the end.

Order

Side effects in predicates

Because evaluation walks from the end, relying on hidden mutable state can surprise you. Prefer pure predicates.

Start

Leading trim

For a prefix controlled by a predicate, use _.dropWhile() or combine findIndex with slice.

❓ FAQ

No. Lodash returns a new array; the source array is unchanged.
Only a suffix: starting at the last index, elements are dropped while the predicate returns truthy. The first falsy result stops the scan; everything to the left is kept.
The whole array is dropped and you get an empty array.
Lodash returns a new empty array.
Use import dropRightWhile from "lodash/dropRightWhile" or require("lodash/dropRightWhile") for tree-shaking friendly bundles.

Summary

Did you know?

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.

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.

6 people found this page helpful