Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] associative arrays question


  • From: javascript@xxxxxxxxxx (Amanda Birmingham)
  • Subject: [Javascript] associative arrays question
  • Date: Fri, 26 Apr 2002 17:21:33 -0600

Use the for ... in construction, like this:

for (var strCurrentKey in theirScore) {
         alert(theirScore[strCurrentKey]);
}

Also works for looping through object properties :)  HTH.

Amanda Birmingham
Web Application Developer

At 06:55 PM 4/26/2002 -0400, you wrote:

>At least, I *think* they're called associative arrays.
>
>I created an associative array in the following manner:
>
>theirScore = new Array(3);
>
>theirScore["Bob"] = 0;
>theirScore["Fred"] = 0;
>theirScore["Sue"] = 0;
>
>This works fine, in the sense that I can fool around with those numeric 
>values like so (where I pass it 'Sue' as an arg):
>
>function boostScore(who) {
>         theirScore[who]++;
>}
>
>..and so on.  The problem I'm running into is at the end, when I want to 
>loop through the theirScore array, I can't seem to get normal indexing to 
>work.  theirScore["Sue"] returns the expected incremented value, but 
>theirScore[2] returns 'undefined'.
>
>How do you loop through an associative array?
>
>_______________________________________________
>Javascript mailing list
>Javascript@xxxxxxxxxx
>https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
>