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.
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
Answer: Use the PHP array_keys() function You can use the PHP array_keys() function to get all the keys out of an associative array.
Associative arrays are arrays that use named keys that you assign to them.
No, you cannot have multiple of the same key in an associative array. You could, however, have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.
var card:Object = {name:"Tom"}; trace("age" in card); // return false trace("name" in card); // return true
Try this operator : "in"
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With