Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Iterating over an object

Posted on 2010-09-21
5
Medium Priority
?
409 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…

604 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