The problem is that:

obj = [];
obj['foo']

is not an associative array, and it is not a hash.  It is an empty
array with the property of foo, identical to this notation:

obj.foo

The length property refers to the items in the array, not the
properties associated with it.

for (each in obj)

iterates over all of the properties/methods in an object, and
Prototype adds a bunch of those to the prototypes of arrays.  It does
not add any to the prototype of an object, so try:

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

for (each in ax) {
    alert(x);
}

Cheers,
Josh Powell

On Apr 2, 8:09 am, "Alex Mcauley" <webmas...@thecarmarketplace.com>
wrote:
> why not push into the json object ?
>
> ----- Original Message -----
> From: "Diodeus" <diod...@gmail.com>
> To: "Prototype & script.aculo.us" <prototype-scriptaculous@googlegroups.com>
> Sent: Thursday, April 02, 2009 4:04 PM
> Subject: [Proto-Scripty] Re: Associative arrays vs hash
>
> 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.
>
> On Apr 2, 10:38 am, Walter Lee Davis <wa...@wdstudio.com> wrote:
> > I wasn't aware that JavaScript arrays could be associative like this.
> > My understanding was that they are always numerically indexed, and if
> > you want an associative "array-like structure" that you can iterate
> > over, you should use a vanilla Object or a Prototype Hash.
>
> > var ax = {'aaa':1,'aab':2,'aac':4,'aad',6}
>
> > for(i in ax){
> > alert( i + ': ' + ax[i] );
>
> > }
>
> > var ax = $H({'aaa':1,'aab':2,'aac':4,'aad',6});
>
> > ax.each(function(s){ alert( s ) });
>
> > Walter
>
> > On Apr 2, 2009, at 10:28 AM, Diodeus wrote:
>
> > > 'm building the following array:
>
> > > var ax = []
> > > ax['aaa'] = 1
> > > ax['aab'] = 2
> > > ax['aac'] = 4
> > > ax['aad'] = 6
>
> > > I would like to loop though all of the values in this array.
--~--~---------~--~----~------------~-------~--~----~
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