6

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

4
  • 1
    that'd be a 2D array. let me illustrate you: [id1 : [optionId1, optionText2], id2: [optionId2, optionText2]] Commented Feb 9, 2011 at 10:37
  • im new to arrays in javascript, how does i create this ? Commented Feb 9, 2011 at 10:43
  • @usoban : what type of array is that u specified ? Commented Feb 9, 2011 at 10:45
  • @usoban : how can i create that arraY that u specified ?, is th a better option than using 3D array, for my requirement ? Commented Feb 9, 2011 at 10:51

4 Answers 4

21

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'
Sign up to request clarification or add additional context in comments.

Comments

2
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.

Comments

1

Okay, so as I said in comment to the question, I think you actually need 2D array. That is, if you have N number of questionIds and each questionId has two properties: optionId and optionText

You can create it somehow like this:

var twoD = new Array();

twoD[1] = new Array();
twoD[1]['optionId'] = 3;
twoD[1]['optionText'] = 'blabla';

twoD[2] = new Array();
twoD[2]['optionId'] = 5;
twoD[2]['optionText'] = null;

alert(twoD[1]['optionId']);

Although note that array with associative key is actually an object in JavaScript.

Update: looping through JavaScript

for(var question : twoD){
 alert(question['optionId']);
 alert(question['optionText']);
}

3 Comments

then where is my questionid stored ?....later i need to convert this array into json string . is that possible?
how can i parse through an associative array ?
Your questionId is 1 or 2 (first dimension in array, so actually you're doing this: twoD[questionId]). Converting to JSON is not a problem, since array can be a json object. As for 'parsing' (actually you're not parsing anything, you're just looping through array), you can achieve this using a for loop. I updated an answer with the solution.
0

hi you can create the nth dimension error in javascript one of the example is below

var apple = new Array(); apple[1] = new Array(); apple[1][1] = new Array(); apple[1][1][1] = 'yourname'

now you can use the recursive function to iterate through the array and check and pare every thing.

4 Comments

is their any better optiona than using 3D array for my requirement given below ?
@kirn No do not create array, create object not an array, array is bit difficult to manage.
my requirement is something like this [id1 : [optionId1, optionText2], id2: [optionId2, optionText2]] and i need to convert this into json string later ?....what type of array or object should i use . Can i use Associative array ? which array type is better for this requirement .
ya you can go with associative array,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.