Skip to content

Instantly share code, notes, and snippets.

@ahafidi
Last active September 27, 2021 12:41
Show Gist options
  • Save ahafidi/c7525efc6f98624a6cab118d6b7c14e1 to your computer and use it in GitHub Desktop.
Save ahafidi/c7525efc6f98624a6cab118d6b7c14e1 to your computer and use it in GitHub Desktop.
some zip implementations
const zip = (...rows) =>
rows[0].map((_, i) => rows.map((row) => row[i]))
const zip2 = (arr, ...arrs) =>
arr.map((val, i) => arrs.reduce((acc, cv) => [...acc, cv[i]], [val]))
const zip3 = (...arrs) => {
let r = []
for (let i = 0; i < arrs.length; i++) {
for (let j = 0; j < arrs[i].length; j++) {
if (!Array.isArray(r[j])) {
r.push([])
}
r[j].push(arrs[i][j])
}
}
return r
};
zip(["a", "b"], [1, 2], [true, false]) // => [['a', 1, true], ['b', 2, false]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment