Skip to content

Instantly share code, notes, and snippets.

@xaliphostes
Last active January 21, 2023 10:40
Show Gist options
  • Save xaliphostes/140b69b2262451be9f2cab75984441b7 to your computer and use it in GitHub Desktop.
Save xaliphostes/140b69b2262451be9f2cab75984441b7 to your computer and use it in GitHub Desktop.
zip multiple arrays in JS
const zip = arrays => {
return Array.apply(null, Array(arrays[0].length)).map( (_,i) => arrays.map( array => array[i] ) )
}
// ----------- testing
const A = [1, 2, 3, 4]
const B = [5, 6, 7, 8]
const C = [9, 4, 3, 2]
console.log( zip([A,B,C]) )
/* result
[
[1, 5, 9],
[2, 6, 4],
[3, 7, 3],
[4, 8, 2]
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment