Solved

Iterating over an object

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SWF won't scale? 4 521
Stock Videos 17 326
Adobe Flash Player - PPAPI plugin 6 544
Virus or Outlook 2007 problem, how to tell? 8 76
This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
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…
The goal of the tutorial is to teach the user how to how to record live broadcast.

863 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

27 Experts available now in Live!

Get 1:1 Help Now