takeWhile es-toolkit
Creates a new array by taking elements from the beginning of the array while the condition function returns true.
Usage
takeWhile(arr, predicate)
Use takeWhile when you only need elements from the beginning of an array that meet a specific condition. It stops taking elements when it encounters the first element that doesn't meet the condition.
It can also be used with object arrays.
Parameters
The array to take elements from.
A condition function called for each element with the element, its index, and the array. Elements are taken while this function returns true.
Returns
Returns a new array containing the elements taken from the beginning while the condition function returns true.
Examples
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.