Solved

Iterating over an object

Posted on 2010-09-21
5
400 Views
Last Modified: 2012-05-10
Hi I have an multiered object (if thats the right word for it, its the same as an associative arry but with an object, please excuse my ignorance lol)

It takes a form as follows

contentsPage.......................................................(object)
            |_cntPgHeader.........................................(object)
                               |_format................................(an instance of a  different class of namespace tlbFormat.pghdrFormat )
                               |          |[inherited]
                               |          |_fontColor
                               |          |_fontSize
                               |
                               |_data....................................(object)
                               |        |_text

As you can see the original object passed ie the contents page object contains a nested intsance of another class. I am trying to iterate over the contents page to retrieve the key pair values (again excuse if wrong terminology).

this works fine for the contents of the data object

 ie for each (var item:* in contentsPage.cntPgheader.data)
                      {trace (item)}

but not for

for each (var item:* in contentsPage.cntPgHeader.format)
                   {trace(item)}

however i can access the values of the format object if I single  out the properties, ie

    trace(contentsPage.cntPgHeader.format.fontColour)

So my question is this how would i go about iterating over the complete conentspage object.

thank in advance

0
Comment
Question by:BloodGrinder
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:bupper
ID: 33727570
It looks like the format is a class (right?) and the data is a basic object. How about doing some type casting to convert the format variable to an object?

Like this:
for each (var item:* in Object(contentsPage.cntPgHeader.format))
                   {trace(item)}

Hope this helps; let me know if it works for you!
0
 
LVL 11

Accepted Solution

by:
petiex earned 500 total points
ID: 33729254
I think that if you want key-value pairs, you need flex introspection
see: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html

As the linked article says, for objects that are not created dynamically, like your format class, you need to use the introspection API.  
import flash.utils.describeType
. . .

for each(var item:* in contentsPage.cntPgHeader.format){
   //print the accessors that are defined
   for each(var x:XML in describeType(item).accessor.@name){
        if (item.hasOwnProperty(String(x)){
            trace(x+": "+item[String(x)]);
        }
   }
   //print the variables that are defined
   for each(var x2:XML in describeType(item).variable.@name){
        if (item.hasOwnProperty(String(x2)){
            trace(x2+": "+item[String(x2)]);
        }
   }

Open in new window

0
 
LVL 11

Expert Comment

by:petiex
ID: 33729268
I left out the closing bracket there. Here it is:
}
0
 

Author Comment

by:BloodGrinder
ID: 33732295
Thanks for both suggestions but no joy as of yet, either way when debugging the excecution just jumps straight over the loop.
0
 

Author Closing Comment

by:BloodGrinder
ID: 33733125
Thanks for the pointer, at present I still cannot get the initial loop to work but i can get data out by stepping down a level in the xml


//print the accessors that are defined
                        for each(var x:XML in describeType(headerFormat)..accessor.@name){
                              if (headerFormat.hasOwnProperty(String(x))){
                                    trace(x+": "+headerFormat[String(x)])
                              }
                        }
                        //print the variables that are defined
                        for each(var x2:XML in describeType(headerFormat)..variable.@name){
                              if (headerFormat.hasOwnProperty(String(x2))){
                                    trace(x2+": "+headerFormat[String(x2)])
                              }
                        }
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question