2 Replies Latest reply on Dec 3, 2008 2:11 AM by ljonny18

    Looping through an objects children

    ljonny18 Level 1
      This post is related to another post I have in the forums: "Dynamically adding containers".

      However, I am using an object (note this object can change - is dynamic):

      [CODE]
      [Bindable]
      public var dashboardDP:Object =
      {item:"dashboard", children: [
      {item:"vbox", id:"vbox 1", children: [
      {item:"panel", id:"panel 1", children: [
      {item:"hbox", id:"hbox 1", children: [
      {item:"view" id:"view 1"},
      {item:"view" id:"view 2"},
      {item:"view" id:"view 3"}
      ]},
      {item:"hbox", id:"hbox 2", children: [
      {item:"view" id:"view 4"},
      ]},
      ]},
      {item:"panel", id:"panel 2", children: [
      {item:"vbox", id:"vbox 2", children: [
      {item:"view" id:"view 5"},
      {item:"view" id:"view 6"},
      ]},
      ]},
      ]},
      ]};
      [/CODE]

      And I want to loop through every child in this object but am struggling to do so :(

      e.g. the following loop will ony return the first child of the object:

      [CODE]
      for(var i:int=0; i<dashboardDP.children.length; i++)
      {
      // some code.....
      }
      [/CODE]

      I want to advance this loop so that it "drills" into and retrieves / reads every child and sub child of each parent etc.....

      Is this possible???

      Any help would be much appreciated on this one.

      Thanks in advance,
      Jon.
        • 1. Looping through an objects children
          xtempore
          Recursion is the answer!

          Write a function that does what you want to do and then call that function from itself for each child.

          Also consider using "for each" rather than "for".

          E.g....
          • 2. Re: Looping through an objects children
            ljonny18 Level 1
            Hi "xtempore" thanks for the fast reply!!!

            Just off the top of my head... I am using an Object type for "dashboardDP".
            Will this work as in your example to loop through and get all the children?

            I notoce you are using a type UIComponent and referencing UIComponent.children - will this work?

            I have train but am getting errors... with the line:
            for each (var c: UIComponent in comp.children) {
            when trying to access the ".children"

            Could you please expand?

            Thanks again,

            Kind Regards,
            Jon