Actionscript 2에서 다이내믹 키로 연결 배열 생성
-
10-07-2019 - |
문제
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] );
그러나 위의 내용은 작동하지 않습니다. 내가 무엇을 놓치고 있습니까?
해결책
나는 배열이 아닌 객체로서의 abuildings 변수를 인스턴스화하는 것으로 시작합니다.
var aBuildings = new Object();
다음으로, 속성을 저장하려는 키에 대한 객체를 먼저 만들어야합니다.
aBuildings[key] = new Object();
aBuildings[key]["property1"] = currentBuilding.someOtherValue;
aBuildings[key]["property2"] = currentBuilding.aDifferentValue;
그런 다음 Abuildings Object에서 값을 읽을 수 있어야합니다.
trace( aBuildings["BUILDING1"]["property2"] );
Building1과 Property2가 문자열 변수가 아닌 경우 문자열 리터럴을 사용해야합니다.
제휴하지 않습니다 StackOverflow