Skip to content

takeRightWhile

Import from @varavel/vdl-plugin-sdk/utils/arrays.

function takeRightWhile<T>(arr, shouldContinueTaking): T[];

Takes elements from the end of the array while the predicate function returns true.

Type Parameters

Type Parameter Description
T Type of elements in the input array.

Parameters

Parameter Type Description
arr readonly T[] The array to take elements from.
shouldContinueTaking (item, index, array) => boolean The function invoked per element with the item, its index, and the array.

Returns

T[]

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

Examples

// Returns [3, 2, 1]
takeRightWhile([5, 4, 3, 2, 1], n => n < 4);
// Returns []
takeRightWhile([1, 2, 3], n => n > 3);
// Using index parameter
takeRightWhile([10, 20, 30, 40], (x, index) => index > 1);
// Returns: [30, 40]