Alex Rivera | Logout

How can I get list of properties in an object in Actionscript?

Asked 2008-12-16T19:05:07.863
31

I have a dataprovider and a filterfunction for my array that's assigned to my dataprovider.

How can I get a list of the properties that are in each row of the dataprovider (item.data) as it gets passed to the filterfunction?

For instance, if my object contained:

  • Object
    • name
    • email
    • address

Then I would want, in my filterfunction to be able to look at name, email and address. Unfortunately, I don't know what these properties will be before hand.

Any ideas?

Edit
Report

1 Answer

2
// this method will work for retrieving properties of a *non-dynamic* (typed) object

// @return - all object properties
public function getProperties(_obj : *) : Array
{
        var _description : XML = describeType(_obj);
        var _properties : Array = new Array();
        for each (var prop:XML in _description.accessor)
        {
                var _property : Object = new Object();
                _property.name = String(prop.@name);
                _property.type = String(simple_type(prop.@type));
                _property.access = String(prop.@access);
                _property.declaredBy = String(prop.@declaredBy);
                try
                {
                   _property.value = _obj[_property.name];
                }
                catch (e : Error)
                {
                   _property.value = "";
                }
                _properties.push(_property)
        }
        _properties.sortOn("name");
        return _properties;
}

// better format for object class information
private function simple_type(_type : String) : String
{
        var lastIndex : int = _type.lastIndexOf("::");
        _type = lastIndex > 0 ? _type.substr(lastIndex + 2) : _type;
        return _type;
}
answered 2012-05-11T23:24:43.827

Your Answer