Creating and using Multi Dimensional Array in Javascript This below code gives three dimensional array creation method and using them.
// This is prototype method which extends
//the functionality of array declaration in javascript
// "x" parameter for second dimension depth,
//"y" for third dimension and "z" for the fourth
Array.prototype.addDim = function(x,y,z) {
for (var i = 0; i < x; i++) {
this[i] = []
for (var j = 0; j < y; j++) {
this[i][j] = []
for (var k = 0; k < z; k++) {
this[i][j][k] = []
}
}
}
return this
}
// normal single dimension array in javascript
var cfStateArr=new Array()
// two dimension array creating method in javascript
//using above prototype extension method
var cfCityArr = [].addDim(1, 0, 0)
// three dimension array creating method in javascript
//using above prototype extension method
var cfCourseArr = [].addDim(1, 3, 0)
//four dimension array creating method in javascript
//using above prototype extension method
var cfSubjectArr = [].addDim(1, 3, 2)
// Array assignments
cfStateArr[0] = "Tamil Nadu"
cfCityArr[0][0]= "Chennai"
cfCityArr[0][1]= "Chengalpattu"
cfCityArr[0][2]= "Coimbatore"
cfCourseArr[0][0][0]= "B.Com."
cfCourseArr[0][0][1]= "B.D.S."
cfCourseArr[0][1][0]= "B.E."
cfCourseArr[0][1][1]= "B.Ed."
cfCourseArr[0][2][0]= "B.E."
cfCourseArr[0][2][1]= "B.Ed."
cfSubjectArr[0][0][0][0]= "Accountancy"
cfSubjectArr[0][0][0][1]= "Commerce"
cfSubjectArr[0][0][1][0]= "Dental Surgery"
cfSubjectArr[0][1][0][0]= "Civil Engineering"
cfSubjectArr[0][1][0][1]= "Electrical & Electronics Engineering"
cfSubjectArr[0][1][1][0]= "Education"
cfSubjectArr[0][2][0][0]= "Civil Engineering"
cfSubjectArr[0][2][0][1]= "Electrical & Electronics Engineering"
cfSubjectArr[0][2][1][0]= "Education"