Check if a key is available in Flex

apache-flexdictionary

I have a dictionary with objects as keys. How can I check if specific object is available in the dictionary?

Best Answer

hasOwnProperty won't work if the key is an object rather than a string.

checking that the value is null won't work if the key is in the dictionary, but with a null value.

The 'in' operator seems to work all the time.

var d:Dictionary = new Dictionary();
var a:Object = new Object();
d[a] = 'foo';
var b:Object = new Object();
d[b] = null;
var c:Object = new Object();
trace(a in d);
trace(b in d);
trace(c in d);

Returns

true
true
false

I believe this is a 'more correct' answer than the one posted above.