-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzip.js
More file actions
40 lines (33 loc) · 1.37 KB
/
zip.js
File metadata and controls
40 lines (33 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*zip_.zip(*arrays)
Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. Use with apply to pass in an array of arrays. If you're working with a matrix of nested arrays, this can be used to transpose the matrix.
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
unzip_.unzip(array)
The opposite of zip. Given an array of arrays, returns a series of new arrays, the first of which contains all of the first elements in the input arrays, the second of which contains all of the second elements, and so on.
_.unzip([["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]);
=> [['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]]*/
function zip(){
var result = [];
for(var i = 0; i < arguments.length; i++){
for(var j = 0; j < arguments[i].length; j++){
if(i == 0){
result.push([]);
}
result[i].push(arguments[j][i]);
}
}
return result;
}
function unZip(arr){
var result = [];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
if(i == 0){
result.push([]);
}
result[i].push(arr[j][i]);
}
}
return result;
}
var myArray = zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);