Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created October 19, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/842287bab87b7d2afb34 to your computer and use it in GitHub Desktop.
Save abozhilov/842287bab87b7d2afb34 to your computer and use it in GitHub Desktop.
js zip
var arr1 = [1, 2, 3, 4],
arr2 = [5, 6, 7, 8, 9];
function* values(arr) {
for (var i = 0, len = arr.length; i < len; i++) {
yield arr[i];
}
}
function* zip(...iters) {
while (true) {
var res = [];
for (var i = 0, len = iters.length; i < len; i++) {
var next = iters[i].next();
if (next.done) {
return;
}
res.push(next.value);
}
yield res;
}
}
for ([k, j] of zip(values(arr1), values(arr2))) {
console.log(k, j);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment