By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,286 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,286 IT Pros & Developers. It's quick & easy.

Creating and accessing a JavaScript multidimensional array?

P: n/a
<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
Share this Question
Share on Google+
5 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
> <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.