# compact

### 说明

&#x20;过滤指定数组中的假值(`0`、`''`、`false`、`null`、`undefined`、`NaN`)

### 源码

<https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/array/compact>

### 用法

{% code title="compact.test.ts" %}

```typescript
import { compact } from "../../utility/array/compact";

describe('compact', () => {
  test('compact should return an empty array when receive an empty array', () => {
    const received: any[] = [];
    const expected = 0;

    const result = compact<any>(received);

    expect(result.length).toBe(0);
  });

  test('compact should return the array only contains truthy value', () => {
    const received: any[] = [true, false, 0, '', undefined, NaN, null, 22];
    const expected = [true, 22];

    const result = compact<any>(received);

    for (const [i, v] of result.entries()) {
      expect(v).toBe(expected[i]);
    }
  });

  test('compact should return a new array', () => {
    const received: number[] = [0, 1, 2, 3, 4];
    const expected: number[] = [1, 2, 3, 4];

    const result = compact<number>(received);

    received.push(5);

    for (const [i, v] of result.entries()) {
      expect(v).toBe(expected[i]);
    }
  });
})
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/utility-array/compact.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
