Skip to content

Instantly share code, notes, and snippets.

@DorkForce
Created January 3, 2016 13:41
Show Gist options
  • Save DorkForce/e57435ec6fb9d1d7afef to your computer and use it in GitHub Desktop.
Save DorkForce/e57435ec6fb9d1d7afef to your computer and use it in GitHub Desktop.
Create Multi-dimensional Array Function
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = createArray.apply(this, args);
}
return arr;
}
createArray(); // [] or new Array()
createArray(2); // new Array(2)
createArray(3, 2); // [new Array(2),
// new Array(2),
// new Array(2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment