Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Last active June 7, 2018 04:05
Show Gist options
  • Save Daniel-Hug/53be05c383eacb1ff8f5fd70e7ce788b to your computer and use it in GitHub Desktop.
Save Daniel-Hug/53be05c383eacb1ff8f5fd70e7ce788b to your computer and use it in GitHub Desktop.
Zip arrays into one | Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc.
// Zip arrays into one
// Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc.
function zipArrays() {
var zipped = [];
var arrays = [].slice.call(arguments);
for (var valueI = 0; arrays.length > 0; valueI++) {
for (var arrayI = 0; arrayI < arrays.length; arrayI++) {
if (arrays[arrayI].length - 1 < valueI) {
arrays.splice(arrayI, 1);
continue;
}
zipped.push(arrays[arrayI][valueI]);
}
}
return zipped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment