如何过滤数组中的 falsy value
Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 503 (opens in a new tab)
Author 回答者: shfshanyue (opens in a new tab)
falsy value 包含:false
, null
, 0
, ""
, undefined
, NaN
使用以下即可过滤
function compact(array) {
return array.filter(Boolean);
}
Author 回答者: justorez (opens in a new tab)
const compact = (array) => array.filter((x) => x);