Maps in ActionScript
Maps are an ActionScript’s built-in associative data type (sometimes called hashes or dicts in other languages).
To create an empty map, use the new Object() or shorthand {} syntax.
Set key/value pairs using typical name[key] = val syntax.
Printing a map with, for example, trace will show all of its key/value pairs.
Get a value for a key with name[key].
If the key doesn’t exist, the undefined value of the value type is returned.
The built-in length property can be emulated using a custom function to count the number of key/value pairs.
Deleting a key/value pair in ActionScript can be done using the delete
keyword.
To remove all key/value pairs from a map, use a custom function to iterate and delete all keys.
The optional second return value when getting a value from a map indicates if the key was present in the map. This can be achieved using the in
keyword.
You can also declare and initialize a new map in the same line with this syntax.
Note that there is no maps
package in ActionScript, but similar functionality can be built using custom functions. Here’s an equivalent to check if two maps are equal:
Note that maps appear in the form {k:v, k:v} when printed with trace
.