Actionscript Hash

Posted by Zachary Pinter on October 09, 2007 at 08:36 PM

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:

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

To loop through all values in a Dictionary:

   1  for each (var item:Object in groupMap)
   2  {
   3     trace(item);
   4  }

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

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

To remove a key/value pair from a Dictionary:

   1  delete obj['key']

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