Skip to content

Instantly share code, notes, and snippets.

@xaliphostes
Last active January 21, 2023 10:39
Show Gist options
  • Save xaliphostes/6da8287de9675232bc98abfa19250d1a to your computer and use it in GitHub Desktop.
Save xaliphostes/6da8287de9675232bc98abfa19250d1a to your computer and use it in GitHub Desktop.
Iterate on many arrays simultaneously
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