can any one help me to create and parse a 3d array in javascript.
im having a questionId each question Id have a selected option and an optional option text.
so i need to create a 3d array for questionId,optionId,optionText(string)..
Thnks in advance
A one dimension array would be:
var myArr = new Array();
myArr[0] = "Hi";
myArr[1] = "there";
myArr[2] = "dude";
alert(myArr[1]); // Alerts 'there'
A two dimensional array would be:
var myArr = new Array();
myArr[0] = new Array("Val", "Va");
myArr[1] = new Array("Val", "yo");
myArr[2] = new Array("Val", "Val");
alert(myArr[1][1]); // Alerts 'yo'
A 3D array is more complicated, and you should evaluate your proposed use of it as it has a limited scope within the web. 99% of problems can be solved with 1D or 2D arrays. But a 3D array would be:
var myArr = new Array();
myArr[0] = new Array();
myArr[0][0] = new Array()
myArr[0][0][0] = "Howdy";
myArr[0][0][1] = "pardner";
alert(myArr[0][0][1]); // Alerts 'pardner'
var data = new Array();
data starts off as a regular one-dimensional array. A two dimensional array is really just an array of arrays. So you could do something like
for (var i = 0; i<numberOfQuestions; i++){
data[i] = new Array();
data[i][0] = something;
data[i][1] = somethingElse;
}
Alternatively you could use the following approach
for (var i = 0; i<numberOfQuestions; i++){
data.push([something, somethingElse]);
}
In any case, at some point you are going to need a loop to populate your initial array with sub-arrays to get the 2d effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With