在Actionscript 2中使用动态键创建关联数组

时间:2009-08-02 13:41:56

标签: 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.buildingCode;

    aBuildings[key][property1] = currentBuilding.someOtherValue;
    aBuildings[key][property2] = currentBuilding.aDifferentValue;
    ... etc
}

以便我可以在以后访问这些数据:

// building description
trace( aBuildings[BUILDING1][property2] );

但上述情况不起作用 - 我错过了什么?

1 个答案:

答案 0 :(得分:2)

我首先将我的aBuildings变量实例化为Object而不是Array:

var aBuildings = new Object();

接下来,您需要首先为要存储属性的键创建一个Object。

aBuildings[key] = new Object();
aBuildings[key]["property1"] = currentBuilding.someOtherValue;
aBuildings[key]["property2"] = currentBuilding.aDifferentValue;

然后你应该能够从aBuildings对象中读取值:

trace( aBuildings["BUILDING1"]["property2"] );

请记住,如果BUILDING1和property2不是String变量,则需要使用String文字。