Skip to content

Function: takeWhile()

ts
function takeWhile<T>(array, predicate): T[];

Takes elements from the start while the predicate returns true. 从数组头部连续取出满足谓词的元素。

Type Parameters

Type ParameterDescription
TItem type / 元素类型

Parameters

ParameterTypeDescription
arrayreadonly T[]Source array / 源数组
predicate(item, index) => booleanTest function / 判断函数

Returns

T[]

Prefix slice / 满足条件的前缀

Example

ts
takeWhile([1, 2, 3, 4], (n) => n < 3)
// => [1, 2]

Since

1.2.0

Released under the MIT License.