[Javascript] associative arrays question

Christopher Basken chriz at basken.com
Fri Apr 26 17:55:30 CDT 2002


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?




More information about the Javascript mailing list