Skip to content

Instantly share code, notes, and snippets.

@gregorynicholas
Forked from Daniel-Hug/zip-arrays.js
Created June 7, 2018 04:05
Show Gist options
  • Save gregorynicholas/3b3fc5ea9c2f47fa458b31c877547118 to your computer and use it in GitHub Desktop.
Save gregorynicholas/3b3fc5ea9c2f47fa458b31c877547118 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