views:

107

answers:

2

Is there a test to see if an Object is an associative array?

Thanks.

+2  A: 

When using an Object as an associative array, you are simply adding dynamic properties to it, with arbitrary values.

for...in loops iterate over only dynamic properties of an Object, so if you create a for...in loop and it completes one loop, you will know that the Object is an associative array.

http://livedocs.adobe.com/flex/3/langref/statements.html#for..in

function isObjectAssociativeArray(obj:Object):Boolean
{
    for (var prop in obj)
    {
        return true;
    } 
    return false;
}
JStriedl
"dynamic" property only exists on Class, to my knowledge.
Glenn
we're talking about 2 different things. the 'dynamic' descriptor in a class definition means it can be assigned 'dynamic properties' at runtime. All generic Objects are by default 'dynamic' and can thus be assigned 'dynamic properties'. ex: var o:Object = {prop1:"value1", prop2:"value2"}; will create a object with 2 dyamic properties o.prop1 and o.prop2.
JStriedl
A: 

You can try getQualifiedClassName and see if the return type is "Object". I haven't tested this myself, but it accepts flash primitives (Object, Array, String...) as well as Classes.

Glenn