README.md
December 5, 2022 ยท View on GitHub
Description
A lot of people have posts on Google asking how to create multidimensional arrays in Javascript. This function is easy to call and will create both one and two dimensional arrays. Please vote if you find that this code saves you time in your array coding. Thanks!
More Info
First Argument: The first dimension of the array
Second Argument (Optional): The second dimension of the array
An array object with the specified dimensions.
| Submitted On | |
| By | Jason Butera |
| Level | Intermediate |
| User Rating | 4.8 (24 globes from 5 users) |
| Compatibility | |
| Category | Data Structures |
| World | Java |
| Archive File |
Source Code
<SCRIPT LANGUAGE="JavaScript">
function CreateArray(dim1) {
if (CreateArray.arguments.length == 1) {
return new Array(dim1);
} else {
var multiArray = new Array(dim1)
for (var i = 0; i < dim1; i++) {
multiArray[i] = new Array(CreateArray.arguments[1]);
}
return multiArray;
}
}
//Create a one-dimensional array
var myArray = CreateArray(5);
//Create a two-dimensional array
var myArray = CreateArray(5,2);
//Looping through the two-dimensional array
for (var i = 0; i <= myArray.length-1; i++) {
for (var j = 0; j < myArray[i].length; j++) {
// alert(myArray[i][j]);
}
}
</SCRIPT>