See the following source code pls:
- var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco”};
- for (var prop in myObject) {
- trace(”myObject.”+prop+” = “+myObject[prop]);
- }
- /* output:
- myObject.firstName = Tara
- myObject.age = 27
- myObject.city = San Francisco
- */
- var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco”};
- for each (var item in myObject) {
- trace(item);
- }
- /* output:
- Tara
- 27
- San Francisco
- */
However, the “for in” statement just “Iterates over the dynamic properties of an object or elements in an array and executes statement for each property or element.”, “To get a list of fixed properties, use the describeType() function, which is in the flash.utils package.”. From the “Flex Help Document”, we can find the class definition of Dictionary as “public dynamic class Dictionary”. It means that the “key” is defined as a dynamic property of class Dictionary. So, we can get the keys through the “for in” statement.
- public static function getKeys(d:Dictionary):Array
- {
- var a:Array = new Array();
- for (var key:Object in d)
- {
- a.push(key);
- }
- return a;
- }
Screenshot:
Click “add to dictionary” to add a new item into the defined Dictionary object.
Change the key name to add more items into the object.
Methods:
1. getKeys()
- public static function getKeys(d:Dictionary):Array
Returns an Array of all keys within the specified dictionary.
Parameters:
d:Dictionary The Dictionary instance whose keys will be returned.
Returns:
Array Array of keys contained within the Dictionary.
2. getValues ()
- public static function getValues(d:Dictionary):Array
Returns an Array of all values within the specified dictionary.
Parameters:
d:Dictionary The Dictionary instance whose values will be returned.
Returns:
Array Array of values contained within the Dictionary.
The following is full source code of DictionaryUtilDemo.mxml:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
- <mx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import com.adobe.utils.DictionaryUtil;
- private var dic:Dictionary = new Dictionary();
- [Bindable]
- private var keys:Array;
- [Bindable]
- private var values:Array;
- private function doAddToDictionary():void
- {
- if(dicKey.text == "" )
- {
- var msg:String = "key of dictionary cannot be empty";
- return;
- }
- dic[dicKey.text] = dicValue.text;
- keys = DictionaryUtil.getKeys(dic);
- values = DictionaryUtil.getValues(dic);
- }
- ]]>
- </mx:Script>
- <mx:VBox width="100%" height="100%">
- <mx:Form>
- <mx:FormItem label="key">
- <mx:TextInput id="dicKey" text="key1" />
- </mx:FormItem>
- <mx:FormItem label="value">
- <mx:TextInput id="dicValue" text="value1"/>
- </mx:FormItem>
- <mx:FormItem>
- <mx:Button label="add to dictionary" click="doAddToDictionary()"/>
- </mx:FormItem>
- </mx:Form>
- <mx:Text text="keys:{ keys.toString()}"/>
- <mx:Text text="values:{ values.toString()}"/>
- </mx:VBox>
- </mx:Application>
来自:http://ntt.cc/2008/09/02/as3corelib-tutorial-how-to-use-dictionaryutil-class-in-flex.html