> For the complete documentation index, see [llms.txt](https://ddzy.gitbook.io/ts-utility-plugins-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/untitled/es6-achieve/_filter.md).

# \_filter

### 说明

模拟实现 `ES6` 的 `Array.filter()` 方法

### 源码

<https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/es6-achieve/filter>

### 用法

{% code title="\_filter.test.ts" %}

```typescript
describe('ES6Achieve._filter tests', () => {
  const _filter = ES6Achieve._filter;

  test('_filter should return a new empty array when receive an empty array', () => {
    const received: number[] = [];

    const result = _filter<number>(received, (v) => {
      return !!v;
    });

    expect(result.length).toBe(0);
    received.push(2);
    received.push(3);
    expect(received.length).toBe(2);
    expect(result.length).toBe(0);
  });

  test('_filter should return the eligible array when receive an array composed of number', () => {
    const received: number[] = [1, 2, 3, 4, 5, 6, 7, 8];
    const expected: number[] = [2, 4, 6, 8];

    const result = _filter<number>(received, (v) => {
      return v % 2 === 0;
    });

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

  test('_filter should return the eligible array when receive an array composed of plain object', () => {
    interface User {
      name: string,
      age: number,
    };
    const received: User[] = [
      { name: 'duan', age: 20 },
      { name: 'zhao', age: 30 },
      { name: 'yang', age: 40 },
    ];
    const expected: User[] = [
      { name: 'zhao', age: 30 },
      { name: 'yang', age: 40 },
    ];

    const result = _filter<User>(received, (v) => {
      return v.age >= 30;
    });

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

  test('_filter should return the aligible array and print the truthy context when receive an array composed of number', () => {
    interface Obj {
      secret: string,
      say: (secret: string) => void,
    };

    const obj: Obj = {
      secret: '980808',
      say() {
        console.log(this.secret);
      },
    };
    const received = [-1, 1, -2, 2, -3, 3];
    const expected = {
      context: obj,
      arr: [1, 2, 3],
    };

    const result = _filter<number, Obj>(received, function (v) {
      expect(this).toBe(expected.context);

      return v > 0;
    }, obj);

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

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/untitled/es6-achieve/_filter.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
