Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Last active July 21, 2021 03:15
Show Gist options
  • Save SethVandebrooke/880143d0e4737e9319f929f015444f1d to your computer and use it in GitHub Desktop.
Save SethVandebrooke/880143d0e4737e9319f929f015444f1d to your computer and use it in GitHub Desktop.
MultiDimensionalArray generation
/**
* 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