Actionscript Hash

On the surface, the Dictionary object in Actionscript seems quite limited compared to the Hash and HashMap libraries of other languages. However, the functionality you would expect is still there, it’s just obscure.

For example, to loop through all keys in a Dictionary:

for (var key:Object in groupMap)
{
   trace(key + ", " + groupMap[key]);
}

To loop through all values in a Dictionary:

for each (var item:Object in groupMap)
{
   trace(item);
}

To check if a key exists (in a way that’s distinguishes from a key whose value could be null):

obj['key'] === undefined  //key exists

To remove a key/value pair from a Dictionary:

delete obj['key']

All of the above code will also work for a regular object treated like a hash.

Leave a Reply