Last active
September 27, 2021 12:41
-
-
Save ahafidi/c7525efc6f98624a6cab118d6b7c14e1 to your computer and use it in GitHub Desktop.
some zip implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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