0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Associative "arrays" serve no useful purpose AFAIK. If you want numeric ordering, use arrays. If you want to access by keys, use object. Also "{}" is a shortcut for "new Object()"
@Chetan Sastry: An Object in Actionscript IS an associative array :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.