Last active
July 21, 2021 03:15
-
-
Save SethVandebrooke/880143d0e4737e9319f929f015444f1d to your computer and use it in GitHub Desktop.
MultiDimensionalArray generation
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
/** | |
* Create multi-dimensional array | |
* @name multiDimensionalArray | |
* @function | |
* @param {number[]} d the depth of each dimension | |
* @example | |
* // create 3 dimensional array, 10 cells deep in every direction (10 by 10 by 10) | |
* multiDimensionalArray([10,10,10]); | |
*/ | |
const multiDimensionalArray = (()=>{ | |
let x = (d = [0], a = [], p = 1) => { | |
for (let i = 0; p <= d.length && i < d[p - 1]; i++) x(d, p == d.length ? (a[i] = null) : (a[i] = []), p + 1); | |
return a; | |
} | |
return x; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment