zip

说明

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

源码

用法

zip.test.ts
1
import { zip } from "../../utility/array/zip";
2
3
describe('Method zip tests...', () => {
4
test('zip should return the zipped array correctly when receive an array composed of number', () => {
5
const received = [
6
[1, 2, 3],
7
[4, 5, 6],
8
[7, 8, 9, 10, 11],
9
];
10
const expected = [
11
[1, 4, 7],
12
[2, 5, 8],
13
[3, 6, 9],
14
[10],
15
[11]
16
];
17
18
const result = zip<number>(...received);
19
20
expect(result.length).toBe(expected.length);
21
22
result.forEach((outerV, outerI) => {
23
outerV.forEach((innerV, innerI) => {
24
expect(innerV).toBe(expected[outerI][innerI]);
25
});
26
});
27
});
28
29
test('zip should return the zipped array correctly when receive an mixed array', () => {
30
const arr1: any[] = [];
31
const obj1 = {
32
name: 'duanzhaoyang',
33
age: 21,
34
}
35
const func1 = function () { };
36
37
const received = [
38
[998, 899, 989, 898],
39
[false, true],
40
[arr1, obj1],
41
[func1]
42
];
43
const expected = [
44
[998, false, arr1, func1],
45
[899, true, obj1],
46
[989],
47
[898],
48
];
49
50
const result = zip<any>(...received);
51
52
expect(result.length).toBe(expected.length);
53
54
result.forEach((outerV, outerI) => {
55
outerV.forEach((innerV, innerI) => {
56
expect(innerV).toBe(expected[outerI][innerI]);
57
});
58
});
59
});
60
});
Copied!
最近更新 1yr ago
复制链接