Created
January 3, 2016 13:41
-
-
Save DorkForce/e57435ec6fb9d1d7afef to your computer and use it in GitHub Desktop.
Create Multi-dimensional Array Function
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
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
From http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript/966938#966938