Arrays 在Actionscript 2中使用动态键创建关联数组
对于XML文件,我想在actionscript中创建一个数组,在这里我可以使用我设置的键引用特定值,而不是0、1、2等Arrays 在Actionscript 2中使用动态键创建关联数组,arrays,actionscript,actionscript-2,associative-array,Arrays,Actionscript,Actionscript 2,Associative Array,对于XML文件,我想在actionscript中创建一个数组,在这里我可以使用我设置的键引用特定值,而不是0、1、2等 buildings = myParsedObjectFromXML; var aBuildings = new Array(); for ( building in buildings ) { var currentBuilding = buildings[building][0]; var key:String = currentBuilding.buil
buildings = myParsedObjectFromXML;
var aBuildings = new Array();
for ( building in buildings ) {
var currentBuilding = buildings[building][0];
var key:String = currentBuilding.buildingCode;
aBuildings[key][property1] = currentBuilding.someOtherValue;
aBuildings[key][property2] = currentBuilding.aDifferentValue;
... etc
}
这样我可以在以后访问数据,如下所示:
// building description
trace( aBuildings[BUILDING1][property2] );
但是上面的方法不起作用-我缺少什么?我首先将aBuildings变量实例化为一个对象,而不是数组:
var aBuildings = new Object();
接下来,需要首先为要在其中存储属性的键创建一个对象
aBuildings[key] = new Object();
aBuildings[key]["property1"] = currentBuilding.someOtherValue;
aBuildings[key]["property2"] = currentBuilding.aDifferentValue;
然后,您应该能够从aBuildings对象读取值:
trace( aBuildings["BUILDING1"]["property2"] );
请记住,如果BUILDING1和property2不是字符串变量,则需要使用字符串文字。+1。关联“数组”在AFAIK中没有任何用处。如果需要数字排序,请使用数组。如果要通过键访问,请使用对象。另外,{}是“new Object()”的快捷方式@Chetan Sastry:Actionscript中的对象是关联数组:)