Creating and accessing a JavaScript multidimensional array?

K

Keiron Waites

<script language="JavaScript" type="text/javascript">
<!--
var array1 = new Array();
var array1i = new Array();

array1[0] = array1i["hello"];

alert(array1[0][0]);
// -->
</script>

Why doesn't this work?

Thanks.
 
L

Lee

Keiron Waites said:
<script language="JavaScript" type="text/javascript">
<!--
var array1 = new Array();
var array1i = new Array();

array1[0] = array1i["hello"];

alert(array1[0][0]);
// -->
</script>

Why doesn't this work?

Because it doesn't make any sense.

I'm guessing that you meant:

<script type="text/javascript">
var array1 = new Array();
var array1i = new Array("hello");
array1[0]=array1i;
alert(array1[0][0]);
</script>
 
D

DB McGee

Not sure if this is what you are going for, but I personally prefer to use
an array of custom JS objects for simulating multi-dimensional arrays:

eg. Imagine a case where you wanted to store in an array, a URL and the text
to display for each URL. I'd start by creating a really simple JS object:

function URL( url, name ) {
this.url = url;
this.name = name;
}

Then you can simply store each object in an 'array of objects':

var myURLs = new Array();
myURLs[0] = new URL ( 'www.yahoo.com', 'Yahoo!' );
myURLs[1] = new URL ( 'www.sitepoint.com', 'Site Point' );
myURLs[3] = new URL ( 'www.cnn.com', 'CNN' );
myURLs[4] = new URL ( 'www.espn.com', 'espn' );

Then you can just access each one via the object properties. Eg. to get the
data for the second value:

url = myUrls[1].url
name = myUrls[1].name

I find that the inherent structure of a custom object approach makes it a
lot easier to manage things - both in developing the code and whenever I
need to revisit it later on to make any changes or extend it in any way.
This is my personal preference anyway.

Apologies in advance if this is confusing or off-topic!
 
D

Douglas Crockford

Then you can simply store each object in an 'array of objects':
var myURLs = new Array();
myURLs[0] = new URL ( 'www.yahoo.com', 'Yahoo!' );
myURLs[1] = new URL ( 'www.sitepoint.com', 'Site Point' );
myURLs[3] = new URL ( 'www.cnn.com', 'CNN' );
myURLs[4] = new URL ( 'www.espn.com', 'espn' );

Or you could use the literal array notation:

var myURLs = [
['www.yahoo.com', 'Yahoo!' ],
['www.sitepoint.com', 'Site Point'],
['www.cnn.com', 'CNN'],
['www.espn.com', 'espn']];

http://www.JSON.org
 
K

Kent Feiler

<!--
var array1 = new Array();
var array1i = new Array();

array1[0] = array1i["hello"];

alert(array1[0][0]);
// -->
</script>

Why doesn't this work?

Thanks.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array1 is only one-diminsional so the second [0] is meaningless. You
may want to do something like this:

var array1 = new Array( new Array(), new Array() );

Now alert( array1[0][0] ); will work.




Regards,


Kent Feiler
www.KentFeiler.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top