JavaScript: Multi-dimensional Array

by Yang Yang on February 3, 2009

In JavaScript, you initiate an array of 3 elements by:

var myArray = new Array(3);

Or:

var myArray = ['', '', ''];

And then to populate them with some values, you need:

for (var i = 0; i < 3; i++) {
	myArray[i] = i;
}

This is pretty much a single dimensional array. To create a multi-dimensional array such as a 2 dimensional one, you need to add one more dimension, initiating yet another array for each of the 1st-dimension elements:

var myArray = new Array(3);

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

A more straightforward approach is to directly assign values to multi-dimensional array variables:

myArray[0][0] = '';
myArray[0][1] = '';
myArray[0][2] = '';
myArray[1][0] = '';
myArray[1][1] = '';
myArray[1][2] = '';
myArray[2][0] = '';
myArray[2][1] = '';
myArray[2][2] = '';
Subscribe to Kavoir: blog feed

You should also read:

Rajiva September 9, 2009 at 9:57 pm

HOw to create like PHP style >>
$new_array=array(0=>”Hi”,1=>”all”,2=>”my”,3=>”name”,4=>”is”,5=>”Rajiva”);

?? Any sugestion?

Yang Yang February 26, 2010 at 4:27 pm

What do you mean?

Andruxa December 1, 2009 at 4:12 pm

$new_array=array(0=>”Hi”,1=>”all”,2=>”my”,3=>”name”,4=>”is”,5=>”Rajiva”);

var new_array = {
0: “Hi”,
1: “all”,
2: “my”,
3: “name”,
4: “is”,
5: “Andrey”
};

Yang Yang February 26, 2010 at 4:27 pm

Does this actually work in PHP?

Dave July 23, 2010 at 10:53 am

Hi,

Thanks for the information. I would suggest never presenting an example when the array is a square, e.g. 3×3. Use 2×3 or 3×4 or whatever. When it’s a square (e.g. 3×3), then when one sees the parameter “3″, it isn’t immediately obvious if that’s the column number or the row number.

Thanks.

Dave

Josh July 11, 2012 at 11:22 am

Actually, there is no such thing as ‘row’ and ‘column’ for arrays.
They are simply ['first dimension identifier'] and ['second dimension identifier']

rick November 18, 2012 at 9:38 am

josh dont be a dick

alec berg December 27, 2010 at 10:53 am

Thanks for the info. I’m usually a VBA/ASP developer and the syntax for arrays is a bit unintuitive.

In vba you just say Dim myArray(3,3)

and your done.

Brad June 14, 2011 at 11:34 pm

are you saying, that the index has to be numeric?

Daniel September 27, 2011 at 3:01 pm

A simpler way to create a multi dimensional array is like this:

var myArray = new Array(new Array(new Array())));

myArray[1][2][3] = “the 3rd dimension”;

Derek April 19, 2012 at 3:53 am

Perfect. Exactly what I was looking for.

Other Daniel June 26, 2012 at 11:57 pm

Same here, using this line to automatically convert a PHP array to javascript

Jon Roland October 13, 2011 at 11:17 am

I want something, such as a jquery script, that executes a python script that does nothing more than immediately display a line of HTML chosen based on the url of the calling HTML page, immediately upon page open or refresh, a different line for each URL. Initially, it can show just the URL, but the eventual solution is to use the URL as a dictionary key to select the line as a value of that dictionary.

Pomax November 2, 2012 at 12:29 am

on a technical note, the use of new Array() is highly discouraged these days, and the format is instead just

var myarray = [];
var i = 3;
while(i–>0) { myarray[i] = i; }

As there is no such thing as a size-restricted array in JavaScript, using new Array(size) actually “lies” about what your code will do.

Additionally, putting a “var i” inside a for loop doesn’t do the same as most programming language: variables are function-scoped, not logic-scoped, so for(var i; …) is the same as

var i;
for(;i<…) { … }

jas April 6, 2013 at 12:08 am

So multi dimensional or 2-dimensional of this would be
var myarray = [[]];
??

Eduardo Mozart de Oliveira December 6, 2012 at 7:39 pm

Thank you! Your post was very useful for me.

Another way to create Multidimensional arrays:


// Adapted from VBScript: Multidimensional array: http://camie.dyndns.org/technical/vbscript-arrays/
// http://www.kavoir.com/2009/02/javascript-multi-dimensional-array.html
var section = new Array(1);
var key = new Array(1);

section[1] = ["i18n"];
key[1] = ["Load"];

// add array key to section.
// array inside array
section[1] = key;

section[1][1] = language;
alert("Section[i18n][Load]: " + section[1][1]);

christophe April 10, 2013 at 1:24 am

var myvar =
{
“yes”:{“a”:['g','h'],’b':['i','j'],’c':['k','l','m']},
“no “:{“a”:['g','h'],’b':['i','j'],’c':['k','l','m']},
};
app.alert(myvar["yes"]["a"]);

Comments on this entry are closed.

Previous post:

Next post: