Last active
January 21, 2023 10:39
-
-
Save xaliphostes/6da8287de9675232bc98abfa19250d1a to your computer and use it in GitHub Desktop.
Iterate on many arrays simultaneously
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 forEach = (as, cb) => { | |
// necessary tests: | |
// - as is an array and all items in as are arrays | |
// - length of all arrays in as are the same | |
as[0].forEach( (serie, i) => cb( as.map( serie => serie[i] ), i, as) ) | |
} | |
// ---------------- testing | |
const A = [1, 2, 3, 4] | |
const B = [5, 6, 7, 8] | |
forEach([A, B], ([a, b]) => { | |
console.log(a, b) | |
}) | |
/* result | |
1 5 | |
2 6 | |
3 7 | |
4 8 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment