> For the complete documentation index, see [llms.txt](https://lodash.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lodash.shujuwajue.com/array/compact.md).

# \_.compact（过滤假值）

**语法：**

```javascript
_.compact(array)
```

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L6874)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.compact)

**描述：**

创建一个数组删除了所有falsey值，`false`, `null`, `0`, `""`, `undefined`, 和 `NaN` 都是 falsey值。

> 解析：移除所有假值并返回一个新的数组

**开始版本：**&#x30;.1.0

**参数：**

* `array (Array)`: 需要处理的数组。

**返回值：**

* `array (Array)`: 返回过滤值后的数组。

**例子：**

```javascript
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
```

## 参考资料

* 源码分析：[每天一个lodash方法-compact](https://segmentfault.com/a/1190000013995861)
