472,127 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Creating and accessing a JavaScript multidimensional array?

<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.
Jul 20 '05 #1
5 28235
> var array1 = new Array();
var array1i = new Array();

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

alert(array1[0][0]); Why doesn't this work?


Perhaps you meant

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

Or

var array1 = [["hello"]];

http://www.crockford.com/survey.html

Jul 20 '05 #2
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>

Jul 20 '05 #3
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!

"Keiron Waites" <we*******@NOSPAMsharemonkey.com> wrote in message
news:bn**********@sparta.btinternet.com...
<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.

Jul 20 '05 #4
> 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

Jul 20 '05 #5
> <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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
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
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by @ nospam.com | last post: by
104 posts views Thread by Leszek | last post: by
38 posts views Thread by djhulme | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.