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?
This will reliably tell you if a particular dictionary is empty:
function isEmptyDictionary(dict:Dictionary):Boolean
{
    for each(var obj:Object in dict)
    {
        if(obj != null)
        {
           return false
        }
    }
    return true;
 }
Note that you need to do the obj != null check - even if you set myDictionary[key] = null, it will still iterate as a null object, so a regular for...in loop won't work in that instance. (If you are always using delete myDictionary[key] you should be fine though). 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With