在 Actionscript 2 中创建具有动态键的关联数组

发布于 2024-08-01 13:08:15 字数 587 浏览 5 评论 0原文

对于 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] );

所示 :不工作 - 我错过了什么?

for an XML file, I want to create an array in actionscript where I can reference a particular value with a key I set rather than 0, 1, 2 etc

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
}

So that I can access the data at a later date like this:

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

but the above isn't working - what am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-08-08 13:08:15

我首先将 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 不是字符串变量,您需要使用字符串文字。

I would start by instantiating my aBuildings variable as an Object rather than an Array:

var aBuildings = new Object();

Next, you need to create an Object first for the key in which you want to store the properties.

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

Then you should be able to read the values from the aBuildings object:

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

Keep in mind that if BUILDING1 and property2 are not String variables you need to use String literals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文