?
Solved

Iterating over an object

Posted on 2010-09-21
5
Medium Priority
?
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

752 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