Skip to content

Instantly share code, notes, and snippets.

@lemanschik
Last active September 30, 2023 05:56
Show Gist options
  • Save lemanschik/4b6a5064c53d0185d9cffd285cac3e4a to your computer and use it in GitHub Desktop.
Save lemanschik/4b6a5064c53d0185d9cffd285cac3e4a to your computer and use it in GitHub Desktop.
ecmascript-array-methods

zipArrays the art of merging two or more arrays of identical length pair wise.

const zip = (arr, ...arrs) => 
  arr.map((val, i) => 
  arrs.reduce((a, arr) => [...a, arr[i]], [val]));

examples:

const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [7, 8, 9];

zip(a, b);                  // [[1, 4], [2, 5], [3, 6]]
zip(a, b, c);               // [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
zip.apply(null, zip(a, b)); // [[1, 2, 3], [4, 5, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment