Skip to content

Function: compact() ​

ts
function compact<T>(array): T[];

Removes all falsy values from an array. 移除数组中的所有假值。

Type Parameters ​

Type ParameterDescription
TThe type of array items / 数组元素类型

Parameters ​

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组

Returns ​

T[]

A new array without falsy values / 移除假值后的新数组

Example ​

ts
compact([0, 1, false, '', 2, null, undefined])
// => [1, 2]

Since ​

1.2.0

Released under the MIT License.