Get size of ActionScript 3 Dictionary

21,106

There is no built-in method to get the size/lenght/count of an AS3 dictionary. There are workarounds: for example, you can create a custom dictionary class which extends or wraps the flash.utils.Dictionary class, adding the counter functionality. You can manage the count as entries are added/removed, or count on-demand using a simple For loop iteration:

public static function countKeys(myDictionary:flash.utils.Dictionary):int 
{
    var n:int = 0;
    for (var key:* in myDictionary) {
        n++;
    }
    return n;
}
Share:
21,106
Bart van Heukelom
Author by

Bart van Heukelom

Professional software developer, online games, full stack but mostly backend. Electronics tinkerer. Maker. Freelance. See LinkedIn for more details. My UUID is 96940759-b98b-4673-b573-6aa6e38272c0

Updated on November 08, 2020

Comments

  • Bart van Heukelom
    Bart van Heukelom over 3 years
    var d:Dictionary = new Dictionary();
    d["a"] = "b";
    d["b"] = "z";

    How to get the length/size of the dictionary (which is 2) ?

  • Bart van Heukelom
    Bart van Heukelom about 14 years
    Too bad, had to build a wrapper.
  • Brilliand
    Brilliand over 12 years
    It was necessary to avoid gotchas in For loops, i.e. what happens when looping over an Array with for..in.
  • LadyCailin
    LadyCailin about 11 years
    Still could have provided a built in DictionaryUtils class.