docs/reference/array/takeRightWhile.md
Returns a new array by taking elements from the end of the array while the condition function returns true.
const result = takeRightWhile(arr, shouldContinueTaking);
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']
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.(T[]): A new array containing the elements taken from the end while the condition function returns true.
// 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]