0

XMLファイルの場合、0、1、2などではなく、設定したキーを使用して特定の値を参照できる配列をactionscriptで作成したいと思います。

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

しかし、上記は機能していません-私は何が欠けていますか?

4

1 に答える 1

2

まず、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が文字列変数でない場合は、文字列リテラルを使用する必要があることに注意してください。

于 2009-08-03T04:58:33.677 に答える