Back to Es Toolkit

takeRightWhile

docs/reference/array/takeRightWhile.md

1.46.11.5 KB
Original Source

takeRightWhile

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

typescript
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.

typescript
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

  • arr (T[]): The array to take elements from.
  • shouldContinueTaking ((item: T, index: number, array: T[]) => boolean): 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

(T[]): A new array containing the elements taken from the end while the condition function returns true.

Examples

typescript
// 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]