21

In ActionScript 3, is there any convenient way of determining if an associative array (dictionary) has a particular key?

I need to perform additional logic if the key is missing. I could catch the undefined property exception, but I'm hoping that can be my last resort.

Soviut
  • 88,194
  • 49
  • 192
  • 260

5 Answers5

38
var card:Object = {name:"Tom"};

trace("age" in card);  //  return false 
trace("name" in card);  //  return true

Try this operator : "in"

Cotton
  • 1,157
  • 9
  • 16
  • Thanks Cotton, I never even knew that operator existed outside of a for-each loop. – BefittingTheorem Apr 01 '09 at 10:53
  • 8
    this makes me happy, its very Pythonic. – Soviut Apr 04 '09 at 22:34
  • 1
    Probably since it's a native keyword. You can always test multiple solutions to see which yields the best performance. Until then, I'd go with the built in solution. – Soviut Oct 16 '12 at 05:30
  • 1
    Just be aware that 'in' has quite low precedence - e.g. this doesn't work as I would expect: `if (! 'key' in obj)` - you need to use `if (! ('key' in obj))` – Richard Nov 27 '14 at 12:11
5

hasOwnPropery is one way you test for it. Take this for example:


var dict: Dictionary = new Dictionary();

// this will be false because "foo" doesn't exist
trace(dict.hasOwnProperty("foo"));

// add foo
dict["foo"] = "bar";

// now this will be true because "foo" does exist
trace(dict.hasOwnProperty("foo"));
Soviut
  • 88,194
  • 49
  • 192
  • 260
Bryan Grezeszak
  • 959
  • 4
  • 6
4

The quickest way may be the simplest:

// creates 2 instances
var obj1:Object = new Object();
var obj2:Object = new Object();

// creates the dictionary
var dict:Dictionary = new Dictionary();

// adding the first object to the dictionary (but not the second one)
dict[obj1] = "added";

// checks whether the keys exist
var test1:Boolean = (dict[obj1] != undefined); 
var test2:Boolean = (dict[obj2] != undefined); 

// outputs the result
trace(test1,test2);
Theo.T
  • 8,905
  • 3
  • 24
  • 38
  • 1
    But does that work if you have no reference to the original object? Cottons answer seems to suit better here. – Mikko Tapionlinna Mar 31 '09 at 06:09
  • Hey, in your question you are mentioning Dictionaries and not Objects or Arrays, am I right ? I haven't tried the "in" operator within a Dictionary instance so far, should be ok. LMK – Theo.T Mar 31 '09 at 09:03
2

hasOwnProperty seems to be the popular solution, but it's worth pointing out that it only deals in strings and can be expensive to call.

If you're using objects as keys in your Dictionary hasOwnProperty will not work.

The more reliable and performant solution is to use strict equality to check for undefined.

function exists(key:*):Boolean {
    return dictionary[key] !== undefined;
}

Remember to use strict equality otherwise entries with a null value but valid key will look empty i.e.

null == undefined // true
null === undefined // false

And actually, as has been mentioned using in should work fine too

function exists(key:*):Boolean {
    return key in dictionary;
}
Mike
  • 1,648
  • 1
  • 16
  • 15
1

Try this:

for (var key in myArray) {
    if (key == myKey) trace(myKey+' found. has value: '+myArray['key']);
}
evilpenguin
  • 5,448
  • 6
  • 41
  • 49