Solved

Iterating over an object

Posted on 2010-09-21
5
396 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
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 how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now