Actionscript 2でダイナミックキーを持つ連想配列を作成する
-
10-07-2019 - |
質問
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が文字列変数でない場合は、文字列リテラルを使用する必要があることに注意してください。
所属していません StackOverflow