Last active
September 30, 2016 14:25
-
-
Save peeke/148b1bed0c6ba5904910 to your computer and use it in GitHub Desktop.
Stitch multiple 2d arrays together to create a single (larger) 2d array.
This file contains 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
stitch2dArrays(arrs) { | |
return arrs.map(row => { | |
return row[0].map((col, i) => row.reduce((prev, current) => prev.concat(current[i]), [])); | |
}).reduce((prev, current) => prev.concat(current), []); | |
} | |
let arr = [[0, 0, 1], | |
[0, 0, 1], | |
[0, 0, 1]]; | |
let stitch = [[arr, arr], | |
[arr, arr]]; | |
let output = stitch2dArrays(stitch); | |
/* output looks like this: | |
output = [[0, 0, 1, 0, 0, 1], | |
[0, 0, 1, 0, 0, 1], | |
[0, 0, 1, 0, 0, 1], | |
[0, 0, 1, 0, 0, 1], | |
[0, 0, 1, 0, 0, 1], | |
[0, 0, 1, 0, 0, 1]]; | |
Useful for procedurally generated game maps! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment