Nav

zip

zip(Array<T>, Array<R>): Array<Array<T | R>>

Given two or more separate lists, the zip function can be used to merge them together into a single list of consecutive n-tuples. Imagine two input lists each being one side of a zipper: similar to the interlocking teeth of a zipper, the zip function interdigitates each element from each input list, one element at a time.

Transform

        
     
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: [0, 1, 2, 3] zip ['a', 'b', 'c', 'd'],
  b: [0, 1, 2, 3] zip ['a'],
  c: [0, 1, 2, 3] zip ['a', 'b']
}
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  'a': [
    [0,'a'],
    [1,'b'],
    [2,'c'],
    [3,'d']
    ],
  'b': [
    [0,'a']
  ],
  'c': [
    [0,'a'],
    [1,'b']
  ]
}

Here is another example of the zip function with more than two input lists.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
payload.list1 zip payload.list2 zip payload.list3
Input

        
     
1
2
3
4
5
6
{
  'list1': ['a', 'b', 'c', 'd'],
  'list2': [1, 2, 3],
  'list3': ['aa', 'bb', 'cc', 'dd'],
  'list4': [['a', 'b', 'c'], [1, 2, 3, 4], ['aa', 'bb', 'cc', 'dd']]
}
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[
  [
    'a',
    1,
    'aa'
  ],
  [
    'b',
    2,
    'bb'
  ],
  [
    'c',
    3,
    'cc'
  ]
]