Not sure I understand why you're doing this but you could try to
individually access each nested object until you get to the desired
object; then you access its content. I rewrote the loop of your code to
do this.
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="vertical">
    <mx:Script>
        <![CDATA[
            public function f():void {
                var t:Object = new Object();
                t.one = [];
                t.one.two = "content";
    
                // Works:
                output1.text = t["one"]["two"];
                
                // Now what?
                var indices:String = "one.two";
        
                var tmp:String = "";
                var obj:Object = t;        // Temporary variable
referencing your root object
                for each (var item:String in indices.split(".")) {
                   if (!obj.hasOwnProperty(item)) break;     // Verifies
that the object actually exists
                   obj = obj[item];
// Now obj references the nested object
                }
                
                output2.text = obj.toString();                // obj
should now reference that last nested object
                
            }
        ]]>
    </mx:Script>
    <mx:Text text="test" id="output1" creationComplete="{f()}" />
    <mx:Text text="test" id="output2" />
</mx:Application>
 
 

________________________________

From: [email protected] [mailto:[email protected]] On
Behalf Of Keith Hughitt
Sent: Thursday, June 04, 2009 2:06 PM
To: [email protected]
Subject: [flexcoders] Dynamically referencing arbitrarily deep arrays?





Does anyone know a method to dynamically index an arbitrarily deep array
or object-literal?

e.g. Given an object "t", and the string "one.two", how can you access
t.one.two?

Here is some sample code to demonstrate the problem:



        <?xml version="1.0" encoding="utf-8"?>
        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="vertical">
            <mx:Script>
                <![CDATA[
                    public function f():void {
                        var t:Object = new Object();
             &nbs p;          t.one = [];
                        t.one.two = "content";
            
                        // Works:
                        output1.text = t["one"]["two"];
                        
                        // Now what?
                        var indices:String = "one.two";
                
                        var tmp:String = "";    
                        for each (var item:String in indices.split("."))
{
                            tmp += "[" + item + "]";
                        }
                        
                        output2.text = t + tmp;                
                     & nbsp;  
                    }
                ]]>
            </mx:Script>
            <mx:Text text="test" id="output1" creationComplete="{f()}"
/>
            <mx:Text text="test" id="output2" />
        </mx:Application>
        
        

Any help would be greatly appreciated.

Thanks!
Keith



Reply via email to