I have stored name of a property of an object as a string.  In PHP, if the object were named $object and the property variable was named $key, it could be accessed with $object->$key.  How can I dynamically access properties in ActionScript?
            Asked
            
        
        
            Active
            
        
            Viewed 6,029 times
        
    3
            
            
        - 
                    1Hmm, x[key] or x.key (the second option will not work if x is strongly typed object.) – uncaught_exceptions Jan 25 '11 at 22:47
2 Answers
7
            You can access it like this:
var obj = {
    property1: 'this is a property',
    property2: 'this is another property'
}
var key = 'property2';
obj[key]; // 'this is another property'
 
    
    
        Jonah
        
- 9,991
- 5
- 45
- 79
2
            
            
        you can access values and properties in your object either by the dot operator or the array access operator:
var myObject:Object = new Object();
myObject.propString = "I'm a String";
myObject.propNumber = 22;
myObject.propObject = {keyOne: "Key String", keyTwo: 23};
trace(myObject["propString"], myObject.propNumber);  //I'm a String 22
trace(myObject.propObject.keyOne, myObject.propObject["keyTwo"]); //Key String 23
the above myObject variable could also be written like this:
var myObject:Object = {propString: "I'm a String", propNumber: 22, propObject: {keyOne: "Key String", keyTwo: 23}};
 
    
    
        Chunky Chunk
        
- 16,553
- 15
- 84
- 162
