On Fri, Apr 18, 2008 at 11:57 AM, Alex Harui <[EMAIL PROTECTED]> wrote:
> See ObjectUtil.getClassInfo
>
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> how would i co about looping over this object in order to access the
> internal objects?
You can also loop over the dynamic properties of your objects with
simple for-in and for-each-in loops.
For example, if you have a complex dynamic object like this:
var someObject :Object = {
anArray: [ 1, 2, 3, 4, 6, "cc" ],
anotherChildObject: { a:"A", b:"B", c:"C" },
nestedChild: {
xx: {x:"X", y:"Y", z:"Z"},
yy: {l:"L", m:"M", n:"N"}
}
}
and you just need to acces it's first level children, you can loop like this:
var child:Object;
for each (child in someObject) {
trace(child);
}
if you need the keys for something (to know that child is named
"anotherChildObject", for example), you can do it like this:
var key :String;
for (key in someObject) {
trace("someObject["+key+"] "+ someObject[key]);
}
This two solutions will work only on your immediate children of your
object; if you need to dig recursively, its as easy as this:
private function traverseChildrenRecursive(object :Object) :void {
trace("--- " + object);
var child :Object;
for each (child in object) {
traverseChildrenRecursive(child);
}
}
These will only work for dynamic properties, though. You should go
with Alex's suggestion if you want to access the non-dynamic
properties (the properties defined by the particular object's class,
not that ones that you 'improvise' at runtime).
Another cool use of that ObjectUtil is to be able to have a simple,
trace-friendly x-ray of whatever object you what. If you want to
quickly figure out the structure of an object, you can always throw a
quick:
trace(ObjectUtil.toString(myWhatever));
cheers,
--
gabriel montagné láscaris comneno
http://rojored.com
t/506.8392.2040