Skip to content

Instantly share code, notes, and snippets.

@ElfhirDev
Last active December 15, 2015 01:49
Show Gist options
  • Save ElfhirDev/5182579 to your computer and use it in GitHub Desktop.
Save ElfhirDev/5182579 to your computer and use it in GitHub Desktop.
JavaScript Multidimension Array Ctor Since creating a two dimensions array in JavaScript is a pain - for me - thanks to Matthew Crumley on Stackoverflow - this constructor of Array matches my idea of multi dimension array ctor.
function createArray(length) {
var a = new Array(length || 0);
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < length; i++) {
a[i] = createArray.apply(this, args);
}
}
return a;
}
// For instance, a 4 rows 6 columns array
var rectangles = createArray(4, 6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment