How to loop through properties of a custom object/VO

If you just have a plain ol boring object, you can loop through it with a basic for…in:

var myObj:Object = {x:1, y:5};
for (var i:String in myObj)
{
    trace(i + ": " + myObj[i]);
}

However, if you have a custom value object, that doesn’t quite work. But what you can do is use describeType() to create an XMLList version on your VO.


var vo : SearchVO = data.getBody() as SearchVO;
// create an XMLList version of the VO
var varList:XMLList = describeType(vo)..variable;

// loop through the property list
for(var i:int; i < varList.length(); i++){
    // output the property name and value
    trace(varList[i].@name+':'+ vo[varList[i].@name]);
}

Post to Twitter


2 Responses to “How to loop through properties of a custom object/VO”

Leave a Reply