Skip to content

Instantly share code, notes, and snippets.

@peeke
Last active September 30, 2016 14:25
Show Gist options
  • Save peeke/148b1bed0c6ba5904910 to your computer and use it in GitHub Desktop.
Save peeke/148b1bed0c6ba5904910 to your computer and use it in GitHub Desktop.
Stitch multiple 2d arrays together to create a single (larger) 2d array.
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