> 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/utility-array/zip.md).

# zip

### 说明

创建一个分组元素的数组, 数组的第一个元素包含所有给定数组的第一个元素, 数组的第二个元素包含所有给定数组的第二个元素, 以此类推. 打包后的数组的长度等于源二维数组的最大长度的项

### 源码

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

### 用法

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

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

describe('Method zip tests...', () => {
  test('zip should return the zipped array correctly when receive an array composed of number', () => {
    const received = [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9, 10, 11],
    ];
    const expected = [
      [1, 4, 7],
      [2, 5, 8],
      [3, 6, 9],
      [10],
      [11]
    ];

    const result = zip<number>(...received);

    expect(result.length).toBe(expected.length);

    result.forEach((outerV, outerI) => {
      outerV.forEach((innerV, innerI) => {
        expect(innerV).toBe(expected[outerI][innerI]);
      });
    });
  });

  test('zip should return the zipped array correctly when receive an mixed array', () => {
    const arr1: any[] = [];
    const obj1 = {
      name: 'duanzhaoyang',
      age: 21,
    }
    const func1 = function () { };

    const received = [
      [998, 899, 989, 898],
      [false, true],
      [arr1, obj1],
      [func1]
    ];
    const expected = [
      [998, false, arr1, func1],
      [899, true, obj1],
      [989],
      [898],
    ];

    const result = zip<any>(...received);

    expect(result.length).toBe(expected.length);

    result.forEach((outerV, outerI) => {
      outerV.forEach((innerV, innerI) => {
        expect(innerV).toBe(expected[outerI][innerI]);
      });
    });
  });
});
```

{% 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/utility-array/zip.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.
