AS3: Check if a Dictionary is empty

actionscript-3, dictionary, flash

Solution

The only way that comes to mind is to iterate through all the keys and count them - like so:

var count:int = 0;

for (var key:Object in dict)
{
   count++;
}

Pretty lame - but I think that is what you are left with. Note that the Dictionary is just a really really really thin wrapper for the vanilla Object.

Problem

Flash implements a dictionary (that is, something like a HashMap) using two approaches. One approach is the `flash.utils.Dictionary` class, and the other is a generic `Object`. I'd like to check how many `key:value` pairs are in the dictionary. In most cases I'd simply like to know it there are any `key:value` pairs, that is, just check if it's empty. The documentation hasn't been much help on this point. Is there a simple and clear way to do this? Failing that, is there an ugly, yet not too brittle way to do this?

Original source