@Diodeus:

> It just seems that it's a bit of a song-and-dance to do something that
> you'd assume would be handled in the native functionality of
> JavaScript arrays.

It's native functionality, but it's native functionality of POJOs
(Plain Old JavaScript Objects) rather than arrays.  In JavaScript, an
array is an object with some extra features around traditional
numerically-indexed arrays.  You don't want that, because you're not
doing numeric indexing.  The term "associative array" is a bit
misleading IMV, really want you want is a "map" or "dictionary" or
"hash" object.  Good news:  That's exactly what a POJO is.  And so you
can do exactly what you wanted to do, quite easily:

* * * *
var p;
var ax = {};
ax['aaa'] = 1;
ax['aab'] = 2;
ax['aac'] = 4;
ax['aad'] = 6;

for (p in ax) {
    alert(p + '=' + ax[p]);
}
// => "aaa=1", "aab=2", "aac=4", "aad=6"
* * * *

The only change I made there was to initialize ax with {} (a blank
object) rather than [] (an empty numeric-index-oriented array) and add
some missing semicolons (never rely on semicolon insertion, it's spawn
of the devil).  "for..in" will iterate over the names of enumerable
properties -- e.g., the keys -- in the object, and blank objects have
no enumerable properties to start with, so only the ones you add get
listed.  (I say the "enumerable" properties because the spec does have
the concept of properties that are not enumerable.  For example, the
array "length" property is not enumerable, and so it doesn't show up
in "for..in" loops.  You can't mark your own properties as not
enumerable, but several intrinsic properties are marked that way.)

You can access those properties with literals, literal strings, or
strings from variables.  So if you do this:

* * * *
var ax = {};
ax['aaa'] = 1;
* * * *

then all of these statements alert "1":

* * * *
alert(ax.aaa);    // Using a literal
alert(ax['aaa']); // Using a literal string
var s = 'aaa';
alert(ax[s]);     // Using a string in a variable
* * * *

@Walter:

> I wasn't aware that JavaScript arrays could be associative like this.

They can indeed, because arrays are objects.  This is perfectly legal:

* * * *
var a = [];
a[0] = "zero";
alert(a.length);
// => "1"
a["flugle"] = "horn";
alert(a.length);
// => "1"
alert(a[0]);
// => "zero"
alert(a["flugle"]);
// => "horn"
* * * *

What I've done there is create an array, which means it has all the
array-related stuff like a 'length' property and a variety of methods
(properties referencing functions) related to doing things with
numerically-indexed arrays.  Then I added a property called 0 to that
array, with the value "zero".  Because the property name is entirely
numeric, the special array behavior kicks in and automatically sets
the 'length' property to 1 (one higher than the highest numeric
property name in the object).  Then I added another property to the
object with the name "flugle" and the value "horn".  Because that
property name is not numeric, it has no effect on the array's 'length'
property at all.  The array-ness of the object completely ignores any
non-numeric properties, but the property is still there, it's still
part of the object.  The non-numeric properties are also completely
ignored if you truncate the array by setting its 'length' property.
Continuing from above:

* * * *
a[3] = "three";
alert(a.length);
// => 4
a.length = 2;
alert(a[3]);
// => undefined
alert(a.length);
// => 2
alert(a["flugle"]);
// => "horn"
* * * *

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Apr 2, 4:39 pm, Diodeus <diod...@gmail.com> wrote:
> Yes, you're right, of course. I'm going to use Szymon's method.
>
> It just seems that it's a bit of a song-and-dance to do something that
> you'd assume would be handled in the native functionality of
> JavaScript arrays.
>
> On Apr 2, 11:30 am, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
> > What's missing is associative arrays in JavaScript. As to adding  
> > elements in a (Prototype) Hash, Szymon already gave you that:
>
> > ax.set('aad',4);
>
> > And there's a matching get() function to pluck an individual element  
> > out of the Hash, too.
>
> > ax.get('aad') //--> 4
>
> > Walter
>
> > On Apr 2, 2009, at 11:04 AM, Diodeus wrote:
>
> > > The problem is in my case, I'm adding elements to the array as I go,
> > > as opposed to building it as a JSON-type hash up front.
>
> > > I'd like to be able to do:
>
> > > ax[newValue] = y
>
> > > Looping through an associative array should be simple, but I'm missing
> > > something here.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to