Identifies the base type of array elements
Identifies the narrowed type when using type guards
The array or array-like object to take from
Function to test each element. Return true to continue, false to stop
OptionalthisArg: anyOptional value to use as this when executing callbackFn
A new array containing elements until the predicate returns false
arrTakeWhile([1, 2, 3, 4, 1], x => x < 3); // [1, 2]
arrTakeWhile([2, 4, 6, 1, 8], x => x % 2 === 0); // [2, 4, 6]
arrTakeWhile([1, 2, 3], x => x > 5); // []
arrTakeWhile([1, 2, 3], x => x < 5); // [1, 2, 3]
// Array-like objects
arrTakeWhile({ length: 4, 0: 1, 1: 2, 2: 3, 3: 4 }, x => x < 3); // [1, 2]
The arrTakeWhile() method returns a new array containing elements from the beginning of the array as long as the predicate function returns true. Once the predicate returns false, iteration stops.