Arrays FLEX:如何获取数组的键名?数组的键名?
我一直在研究如何获取flex数组的键名,因为Array类中没有这样的函数。这是我的答案,可能对你们任何人都有用。Arrays FLEX:如何获取数组的键名?数组的键名?,arrays,apache-flex,key,Arrays,Apache Flex,Key,我一直在研究如何获取flex数组的键名,因为Array类中没有这样的函数。这是我的答案,可能对你们任何人都有用。import mx.utils.ObjectUtil; import mx.utils.ObjectUtil; var __arCurrent:Array=new Array(); __arCurrent["x"]=10; __arCurrent["y"]=20; //Get class info of __arCurrent to get
import mx.utils.ObjectUtil;
import mx.utils.ObjectUtil;
var __arCurrent:Array=new Array();
__arCurrent["x"]=10;
__arCurrent["y"]=20;
//Get class info of __arCurrent to get the keys name of the array
var classInfoArray:Object=ObjectUtil.getClassInfo(__arCurrent);
//For each key of the array __arCurrent
for(var i:int=0;i<classInfoArray.properties.length;i++){
var keyname:String = classInfoArray.properties[i].localName;
}
var__arCurrent:Array=newarray();
__电弧电流[“x”]=10;
__电弧电流[“y”]=20;
//获取uu arCurrent的类信息以获取数组的键名称
var classinfo数组:Object=ObjectUtil.getClassInfo(\uu arCurrent);
//对于数组的每个键uu arCurrent
对于(var i:int=0;iimport mx.utils.ObjectUtil;
var__arCurrent:Array=newarray();
__电弧电流[“x”]=10;
__电弧电流[“y”]=20;
//获取uu arCurrent的类信息以获取数组的键名称
var classinfo数组:Object=ObjectUtil.getClassInfo(\uu arCurrent);
//对于数组的每个键uu arCurrent
对于(var i:int=0;i请尝试以下方法:
protected function getArrayKeys(a:Array):void
{
for (var key:String in a)
{
trace (key);
}
}
编辑:
我创建了示例应用程序来演示关联数组
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var o1:Object = new Object();
var o2:Object = new Object();
//these access methods are equivalent:
//By property
o1.x = "Object1 xValue";
o1.y = "Object1 yValue";
//By indexer
o2["x"] = "Object2 xValue";
o2["y"] = "Object2 yValue";
//Access object properties by name
trace (o1.x);
trace (o1.y);
trace (o2.x);
trace (o2.y);
//dumpObject uses indexer to access key values
dumpObject(o1);
dumpObject(o2);
}
protected function dumpObject(o:Object):void
{
for (var key:String in o)
{
//loop through property names and access values by indexer.
trace ("Key = " + key + ". Value = " + o[key]);
}
}
]]>
</fx:Script>
</s:Application>
另外请注意,使用Object时,键类型仅限于字符串。可以使用除使用字典类的字符串外的键类型创建关联数组。尝试以下操作:
protected function getArrayKeys(a:Array):void
{
for (var key:String in a)
{
trace (key);
}
}
编辑:
我创建了示例应用程序来演示关联数组
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var o1:Object = new Object();
var o2:Object = new Object();
//these access methods are equivalent:
//By property
o1.x = "Object1 xValue";
o1.y = "Object1 yValue";
//By indexer
o2["x"] = "Object2 xValue";
o2["y"] = "Object2 yValue";
//Access object properties by name
trace (o1.x);
trace (o1.y);
trace (o2.x);
trace (o2.y);
//dumpObject uses indexer to access key values
dumpObject(o1);
dumpObject(o2);
}
protected function dumpObject(o:Object):void
{
for (var key:String in o)
{
//loop through property names and access values by indexer.
trace ("Key = " + key + ". Value = " + o[key]);
}
}
]]>
</fx:Script>
</s:Application>
还要注意的是,使用Object时,键类型仅限于字符串。除了使用字典类的字符串外,还可以使用键类型的关联数组。如果它对您有帮助,请发表评论。:)如果它对您有帮助,请发表评论。:)我不会将数组类用作关联数组。Use Object或Dictionary Instedi不会将数组类用作关联数组。使用Object或Dictionary代替。你也可以用Object代替Array。我不知道它是如何工作的,但我想它的工作原理也是最快的!:)在AS3中,for(…In)通过对象属性循环,但对于每个(…In)通过数组或集合项循环。顺便说一句,当您将数组用作关联数组时,它实际上会在基础对象中创建属性,因为数组也是从对象派生的。因此,正如上面提到的Florian,没有理由将数组实例用于关联数组。感谢您提供的所有信息。如果我理解的很好,你的意思是没有理由实例化一个数组来拥有一个关联数组?你的意思是你可以只实例化一个对象?你是怎么做到的?你也可以用对象而不是数组来做到这一点。我不知道它是如何工作的,但我想它的工作原理也是最快的!:)在AS3中,for(…In)通过对象属性循环,但对于每个(…In)通过数组或集合项循环。顺便说一句,当您将数组用作关联数组时,它实际上会在基础对象中创建属性,因为数组也是从对象派生的。因此,正如上面提到的Florian,没有理由将数组实例用于关联数组。感谢您提供的所有信息。如果我理解的很好,你的意思是没有理由实例化一个数组来拥有一个关联数组?你的意思是你可以只实例化一个对象?你是怎么做到的?