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

8

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?

Suggestibility answered 16/12, 2008 at 19:5 Comment(0)
F
54

If it's a dynamic object I believe you can just do something like this:

var obj:Object; // I'm assuming this is your object

for(var id:String in obj) {
  var value:Object = obj[id];

  trace(id + " = " + value);
}

That's how it's done in AS2, and I believe that still works for dynamic objects in AS3. I think the properties that it will show is more limited on non-dynamic objects.

Female answered 16/12, 2008 at 19:40 Comment(3)
Perfect! Works great. Been trying to figure that out for days. Thanks very much! G-ManSuggestibility
+1 Even AS3 doesn't have eval anymore, a thing like this make it dynamic.Inearth
"I think the properties that it will show is more limited on non-dynamic objects." <-- Looks like you're right about this, it seems like such a for loop won't go through statically-defined properties of a class.Elburr
F
10

flash.utils.describeType(value:*) will also give you a list of properties on an object.

Filling answered 28/10, 2009 at 22:36 Comment(0)
O
7

You are probably looking for

ObjectUtil.getClassInfo(object) 

,see:

http://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#getClassInfo%28%29

Be aware that there is a bug in it which causes it to treat XML as a non-dynamic data type. More on the bug in: bugs.adobe.com/jira/browse/SDK-17712

Osteoma answered 16/12, 2008 at 19:5 Comment(0)
M
7

for-in works for dynamic objects only. For typed objects you need to use some kind of reflection to get property names (e.g. http://www.as3commons.org/as3-commons-reflect/index.html)

/Andrei.

Margartmargate answered 17/7, 2009 at 14:8 Comment(0)
L
4

for me was useful only this:

trace('obj = '+getProperties(obj));

        public static function getProperties(obj:*):String  {
            var p:*;
            var res:String = '';
            var val:String;
            var prop:String;
            for (p in obj) {
                prop = String(p);
                if (prop && prop!=='' && prop!==' ') {
                    val = String(obj[p]);
                    if (val.length>10) val = val.substr(0,10)+'...';
                    res += prop+':'+val+', ';
                }
            }
            res = res.substr(0, res.length-2);
            return res;
        }

and you get something like this:

obj = m:email@ra..., r:true
Launderette answered 11/9, 2012 at 23:14 Comment(1)
This one worked for me. I was deserializing a JSON from my web api in an Object and the other methods listed here were not working (I tried all the others). Thanks!Unifoliolate
L
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;
}
Leia answered 11/5, 2012 at 23:24 Comment(1)
how is simple_type declared?Plenteous
G
1

you can use a for .. in loop to get the properties names, or a for each .. in loop to get the property values ...


for( var o : * in object){
    trace( o + " = " + object[o] );
}
/************* OR ******************/
for each( var o : * in object ){
    trace( "object has property: " + o );
}
Gastongastralgia answered 23/12, 2008 at 21:21 Comment(0)
H
1

This also will help you..
1. for Loop - Will work based on index
2. for each - recursive call upto the length
3. for in - used to read the property values

     for( var obj : String in objectData ) //here objectData is your object
     {
        trace( "Object Name Is : " + obj );
        var data : Object = objectData[obj]; //here we get the object value by using the property name
        trace( "Value Is : " + data ); //Converts object to string
     }
Halfbaked answered 13/2, 2015 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.