Create a dynamic two dimensional array in Javascript

Posted on 2002-05-21
Last Modified: 2008-01-16

I am trying to create a dynamic two dimensional array in JavaScript.

For example I know that I need 6 columns - it's not even necessary to put this in if it's possible to do without.

I know that it's simple to create a one dimensional array as follows:

var myArray=new Array();

However, to create a two dimensional array - the only thing I can find on the net seems to want me to specify the size of it as follows:

var myArray=new Array();
for(i=0; i<3; i++)
   myArray[i]=new Array()

However, I don't want to have to put it into a limited loop - I want it dynamic.

Would appreciate any suggestions forthcoming!!!

Question by:NoelleBlaney

Expert Comment

ID: 7024495
Take a look at

I think that they both do what you want and are pretty easy to understand. (And I wrote them)


Author Comment

ID: 7024556
In your example, you have a set size for your array. You are specifying the row size for your array by using the following:

var ref = new Array();
And then referencing the elements in the array by saying ref[1]= whatever data you want to put into it.

I want to be able to do the following:

var myArray=new Array();
for(i=0; i<3; i++)
  myArray[i]=new Array()

but without specifying the 3 in the loop - I want this size to be dynamic - obviously it doesn't have to be in a for loop but something to that effect. This will create an array with a second dimension which will grow as we add data.

Again, any suggestions are welcome!!

Expert Comment

ID: 7025038
Ummm... and that doesn't work?  Because I do something in a page that's really similar.  Mine looks like this:

var myArray = new Array();
for( var i = 0; i < 3; i++ ){
   var myRecord = new Array();
   myRecord[0] = "Some information";
   myArray[i] = myRecord;

That's psudo-code of course - I was doing stuff with recordsets, so myRecord had multiple records, and the "3" was actually an EOF, but this is the gist of it.  I think the major difference between what you posted and this is that I declare the second array, then assign it to the first array, rather than doing both at once.

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


Expert Comment

ID: 7025078
Oh wait... I think maybe I misunderstood your question.  All you want is an array that you can append data to at any time?  As opposed to a C array where you define the amount of memory when you declare it and can never change it?  In that case, don't worry about it.  Array() is dynamic by nature.  You don't have to declare a size for it - ever.  Just go ahead and keep adding new arrays.  However, this does require some responsibility on your part - you've got to make sure that you add stuff to the end instead of overwritting existing info (you can use .length to do that).  So whenever you've got a new array you want to add to the main array, you'd have something like this:

var myArray = new Array();
//...some code
var iSize = myArray.length;
myArray[iSize] = new Array();

Does that answer your question?


Expert Comment

ID: 7026486
listening ....

Accepted Solution

iDeb earned 50 total points
ID: 7028369
almost on the same lines as allie:-

var myArray=new Array();
for(i=0; i<myArray.length; i++)
 myArray[i]=new Array()

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now