Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating and parsing a 3D array in javascript? [duplicate]

Tags:

javascript

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

like image 779
kiran Avatar asked Nov 29 '22 18:11

kiran


2 Answers

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'
like image 78
Tom Gullen Avatar answered Dec 20 '22 20:12

Tom Gullen


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.

like image 26
Joe Green Avatar answered Dec 20 '22 22:12

Joe Green