Looping Through an Object in Flash ActionScript 3.0

Here’s an oldie but a goodie. You’re working in Flash, making calls to retrieve data from a server, and you get back an Object rather than XML. No problem! You can still loop through the Object just fine with a slight modification to your loop logic you’re used to using when parsing through XML. In the sample below, you can obviously assume that “myObject” is the Object you’re looping through. Also, the example code is just set to trace out the attribute name and value. Beyond that, you can do anything you want with the data. Here’s how your loop will look:

for each (var attr:* in myObject.elements()) {
  trace(“Object Name: “ + attr.name());
  trace(“Object Value: “ + attr);

And that’s all there is to it. Simple and straightforward. But a good reference to have the next time you have to loop through and Object.

Until next time, happy coding!

There are no comments yet, add one below.

Leave a Comment