No associative arrays in JavaScript?

While working on the conversion of http://www.winningsideministries.org to smarty, I ran across an interesting little caveat in javascript (and therefore JSON).  There are no associative arrays.  Members of an object can be accessed in associative array syntax, but there are no associative arrays.  This confused the HECK out of me.  I was very frustrated when the following code:

var myArray = new Array();
myArray["index"] = 23;
myArray["recipient"] = "joe";
myArray["body"] = "he got sick";
print(JSON.stringify(myArray));

Returned the following:

"[]"

I even filed a bug report with Mozilla, thinking that the built-in JSON handler was flawed.  This was not the case.  I was informed by the maintainer to whom my bug was assigned that there is no such thing as an associative array in javascript.  I was pretty bewildered to find this out, given the number of websites that describe how to use them.  Apparently a lot of people out there (like me) don’t know what they’re talking about.

It turns out that in javascript, you can very well do the following:

myArray = new Object();
myArray.someProperty = "duh";
alert(myArray["someProperty"]);

And you’ll get exactly what you expect: duh will be printed in the alert box.  I have no idea why someone thought this was a good idea, but it hurts my head.  I also don’t know why an object that is built that way can’t be parsed into JSON, but maybe it has something to do with the way the Array object type works.

Advertisements

2 Responses to No associative arrays in JavaScript?

  1. I hate programming in JavaScript. I really do. Perhaps it’s just because it’s so easy to misspell a work or something similar and JavaScript doesn’t care at all (well, I guess most scripting languages don’t care).

    Have you ever used Prototype? http://www.prototypejs.org/ For any “serious” JavaScript programming I’ve always used it. I like it quite a bit. node.js at http://nodejs.org/ is also pretty interesting–server side JavaScript.

  2. Lytithwyn says:

    I actually use jQuery for most of my javascript stuff. There is even a plugin for jQuery called jquery-json that I’m using. It really makes json a lot easier to work with (if you know what you’re doing, unlike me!). It even checks to see if the browser currently supports native json functions (eg., Firefox 3.1+) and will opt to use their functions instead of the slower ones implemented by the plugin.

    I really like the ajax interface in jQuery, too.

    Thanks for the recommendation, though!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: