es-toolkit/docs/reference/array/takeRightWhile.md

29 lines
739 B
Markdown
Raw Normal View History

2024-04-25 14:56:13 +03:00
# takeRightWhile
Takes elements from the end of the array while the predicate function returns `true`.
## Signature
```typescript
function takeRightWhile<T>(arr: T[], shouldContinueTaking: (item: T) => boolean): T[];
2024-04-25 14:56:13 +03:00
```
### Parameters
2024-04-25 14:56:13 +03:00
- `arr` (`T[]`): The array to take elements from.
- `shouldContinueTaking` (`(item: T) => boolean`): The predicate function that is called with each element. 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 predicate returns `true`.
## Examples
```typescript
// Returns [3, 2, 1]
takeRightWhile([5, 4, 3, 2, 1], n => n < 4);
// Returns []
takeRightWhile([1, 2, 3], n => n > 3);
```