1

I now multi-dimensional array in javascript is a bit silly. But in my program I have to use it. I want to declare a dynamic 3 columns and m-rows array using javascript and then in a loop i need to insert some element to that array. finally i want to display that array.

   var x = new Array();
    for (var y=0; y<=counter; y++) {
    x[y] = new Array (3);
x[y][0]='a';
x[y][1]='b';
x[y][2]='c';
    }

your help is highly appreciated ...

1
  • plase provide some code... what have you tried? Commented May 17, 2012 at 11:10

4 Answers 4

1

arrays grow as needed and so there is no point in declaring the length of the array. what you started with is fine. http://books.google.ca/books?id=4RChxt67lvwC&pg=PA141&lpg=PA141&dq=JS+array+grow+as+needed?&source=bl&ots=tgY8BlGXm8&sig=_3jcH1LTmYi9QiXxn9pHROpbN1s&hl=en&sa=X&ei=7v60T_XwA4ThggfGuoEX&ved=0CEoQ6AEwAQ#v=onepage&q=JS%20array%20grow%20as%20needed%3F&f=false

http://www.codingforums.com/showthread.php?t=5198

just a heads up it will create an array that is 1 bigger then counter. this is b/c in your for loop you have y<=counter and y starting at 0. if you want the length to be counter change it to

    y<counter

in order to display the array you might want to consider a for nested loop.

JS

for(var i=0; i<x.length; i++)
  for (var j=0; j<3; j++)
    alert(x[i][j]);

where x is the reference to the array. if you want to print the entire array at once consider creating a string from the elements in the array and print that

function displayArray(x){
var stringArray='';
 for(var i=0; i<x.length; i++)
  for (var j=0; j<3; j++)
    stringArray+= x[i][j];
 alert(stringArray);

}

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this?

var x = [
    [
        [1, 2, 3],
        [4,5,6]
    ],
    [
        [7, 8, 9]
    ]   
];

alert(x[0][1][2]);

This is one reason why JavaScript's variable syntax isn't always what it's cracked up to be. Compare to Java:

String[][][] x = new String[5][5][5];

1 Comment

There are two errors included in this answer. First the TO wants a dynamic approach for declaring the array. Second the comparison with Java is absolutley useless
0

Something like this maybe?

var sDataArray=MultiDimensionalArray(7,2); 
alert(sDataArray[0][0]); 

function MultiDimensionalArray(iRows,iCols) 
{ 
var i; 
var j; 
   var a = new Array(iRows); 
   for (i=0; i < iRows; i++) 
   { 
       a[i] = new Array(iCols); 
       for (j=0; j < iCols; j++) 
       { 
           a[i][j] = ""; 
       } 
   } 
   return(a); 
}

Being dynamic in rows maybe you want a list of arrays? You can also do a method to add a line, and increase the array size.

Comments

0
try something like this..

var cols = 3;
var rows = 5;

createArray(cols,rows);

function createArray(cols,rows) {
    var newArray = [];
    for(i=0;i<cols;i++) {
        newArray[i] = [];
        for(j=0;j<rows;j++) {       
            newArray[i][j] = "["+i+"]"+"["+j+"]";
        }

    }   
    return newArray;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.