Calculating Dictionary length in Flex
Asked Answered
K

6

7

What's the best way to calculate the length of a Dictionary object in Flex?

var d:Dictionary = new Dictionary();
d["a"] = "alpha";
d["b"] = "beta";

I want to check the length which should be 2 for this Dictionary. Is there any way to do it other than looping through the objects?

Kitchenware answered 1/4, 2009 at 21:14 Comment(0)
A
12

No, there is no way to check the length of an object(Dictionary is pretty much an object that supports non-String keys) other than looping through the elements.

http://www.flexer.info/2008/07/31/how-to-find-an-objects-length/

You probably don't have to worry about checking if the property is an internal one.

Alessandro answered 1/4, 2009 at 21:22 Comment(1)
Here is the code to loop as notbody else provided it: '[ public static function getLen(d:Dictionary):int { var i:int = 0; for (var key:Object in d) { i++; } return i; } ]Ratty
K
3

There's a util function in as3corelib which can get the keys in the dictionary. You can check out DicitonaryUtil

The method is:

    public static function getKeys(d:Dictionary):Array
    {
        var a:Array = new Array();

        for (var key:Object in d)
        {
            a.push(key);
        }

        return a;
    }

So you would do getKeys(dictionary).length

Keratitis answered 4/4, 2009 at 19:56 Comment(1)
This actually cretes an array and adds each element to it so this is very expensive to do (in memory and time)Ratty
P
1

You can use associative arrays instead because I don't think it's possible to check the length of a Dictionary object. You could however extend the Dictionary class and add that functionality and override the corresponding methods.

Alternatively, you could loop through it each time to get the length which isn't really a good idea but is available.

var d:Dictionary = new Dictionary();
d["hi"] = "you"
d["a"] = "b"
for (var obj:Object in d) {
  trace(obj);
}
// Prints "hi" and "a"

You can also look here for information on using the "setPropertyIsEnumerable" but I believe that's more useful for objects than it is for Dictionary.

Peugia answered 1/4, 2009 at 21:48 Comment(0)
O
0

You could write a class around a dictionnary that controls insertions/removals so you can keep track of the key count.

Try extending proxy or just do a wrapper.

Owens answered 2/4, 2009 at 13:34 Comment(0)
W
0

For anyone stumbling upon this now there is an update to DictionaryUtil. You can now just call..

var count:int = DictionaryUtil.getKeyCount(myDictionary);
Wacker answered 21/7, 2010 at 19:5 Comment(3)
Do you have information about when this became available or a link to documentation? There's not much information out there about this.Kitchenware
No I don't. I know that it's available in Flex 3.5 and in 4, probably even as far back as 3. I was just looking for this answer and someone on here mentioned DictionaryUtil so I did auto-complete on the functions available in it and stumbled upon this.Wacker
I'm using Flex 4 and can't get this API. But there is DictionaryUtil.getItemCount in Flit library. code.google.com/p/flit/source/browse/trunk/flit/src/obecto/…Kronfeld
M
0

You can get keys of the Dictionary and check the length of keys array as below:

var d:Dictionary = new Dictionary();
d["a"] = "alpha";
d["b"] = "beta";
var count:int = DictionaryUtil.getKeys(d).length;
Mira answered 23/2, 2017 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.