質問

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

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

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top