Removes falsy values from an array.
Returns a new array containing only the truthy elements of the input array. Falsy values are false, 0, 0n, '', null, undefined, and NaN.
false
0
0n
''
null
undefined
NaN
过滤掉数组中的假值(falsy)元素。
返回一个新数组,仅包含输入数组中的真值元素。假值包括 false、0、0n、''、null、undefined 和 NaN。
The type of truthy elements in the array / 数组中真值元素的类型
The array to compact. 要过滤的数组
A new array with all falsy values removed. 移除所有假值后的新数组
O(n) time complexity - single pass through the array
O(n) 时间复杂度 - 单次遍历数组
compact([0, 1, '', null, undefined, NaN])// => [1] Copy
compact([0, 1, '', null, undefined, NaN])// => [1]
Removes falsy values from an array.
Returns a new array containing only the truthy elements of the input array. Falsy values are
false,0,0n,'',null,undefined, andNaN.过滤掉数组中的假值(falsy)元素。
返回一个新数组,仅包含输入数组中的真值元素。假值包括
false、0、0n、''、null、undefined和NaN。