For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/takeRightWhile.md.

takeRightWhile es-toolkit

Returns a new array by taking elements from the end of the array while the condition function returns true.

const result = takeRightWhile(arr, shouldContinueTaking);

Usage

takeRightWhile(arr, shouldContinueTaking)

Use takeRightWhile when you want to take elements from the end of an array that meet a condition. It stops when it encounters the first element for which the condition function returns false.

import { takeRightWhile } from 'es-toolkit/array';

// Take numbers less than 4 from the end
takeRightWhile([5, 4, 3, 2, 1], n => n < 4);
// Result: [3, 2, 1]

// Take numbers greater than 3 from the end
takeRightWhile([1, 2, 3], n => n > 3);
// Result: []

// Take elements with string length <= 5
takeRightWhile(['hello', 'world', 'foo', 'bar'], str => str.length <= 5);
// Result: ['hello', 'world', 'foo', 'bar']

Parameters

arrT[]required

The array to take elements from.

shouldContinueTaking(item: T, index: number, array: T[]) => booleanrequired

A condition function called with each element, its index, and the array. Elements are included in the result as long as this function returns true.

Returns

A new array containing the elements taken from the end while the condition function returns true.

T[]

Examples

// Using index parameter
takeRightWhile([10, 20, 30, 40], (x, index) => index > 1);
// Returns: [30, 40]

// Using array parameter
takeRightWhile([1, 2, 3, 4], (x, index, arr) => x >= arr.length / 2);
// Returns: [2, 3, 4]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.