Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an associative array has a key?

Tags:

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.

like image 838
Soviut Avatar asked Mar 29 '09 19:03

Soviut


People also ask

How do I check if an array contains a key?

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.

How do you find the key of an associative array?

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.

What is key in associative array?

Associative arrays are arrays that use named keys that you assign to them.

Can associative array have same key?

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.


2 Answers

var card:Object = {name:"Tom"};  trace("age" in card);  //  return false  trace("name" in card);  //  return true 

Try this operator : "in"

like image 144
Cotton Avatar answered Nov 02 '22 14:11

Cotton


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")); 
like image 28
Bryan Grezeszak Avatar answered Nov 02 '22 13:11

Bryan Grezeszak