【问题标题】:How can I get list of properties in an object in Actionscript?如何在 Actionscript 中获取对象的属性列表?
【发布时间】:2010-09-27 04:53:45
【问题描述】:

我的数组有一个 dataprovider 和一个 filterfunction,分配给我的 dataprovider。

当数据提供者 (item.data) 被传递给过滤器函数时,如何获取其每一行中的属性列表?

例如,如果我的对象包含:

  • 对象
    • 姓名
    • 电子邮件
    • 地址

然后我希望在我的过滤功能中能够查看姓名、电子邮件和地址。不幸的是,我事先不知道这些属性是什么。

有什么想法吗?

【问题讨论】:

    标签: actionscript-3 flex3 filterfunction


    【解决方案1】:

    这也将帮助你..
    1. for 循环 - 将基于索引工作
    2. 对于每个 - 递归调用到长度
    3. for in - 用于读取属性值

         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
         }
    

    【讨论】:

      【解决方案2】:

      flash.utils.describeType(value:*) 还会为您提供对象的属性列表。

      【讨论】:

        【解决方案3】:
        // 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;
        }
        

        【讨论】:

        • simple_type 是如何声明的?
        【解决方案4】:

        对我来说只有这个有用:

        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;
                }
        

        你会得到这样的结果:

        obj = m:email@ra..., r:true
        

        【讨论】:

        • 这个对我有用。我在一个对象中从我的 web api 反序列化 JSON 并且此处列出的其他方法不起作用(我尝试了所有其他方法)。谢谢!
        【解决方案5】:

        你可能正在寻找

        ObjectUtil.getClassInfo(object) 
        

        ,见:

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

        请注意,其中存在一个错误,导致它将 XML 视为非动态数据类型。 更多关于错误的信息: bugs.adobe.com/jira/browse/SDK-17712

        【讨论】:

          【解决方案6】:

          for-in 仅适用于动态对象。对于类型化的对象,您需要使用某种反射来获取属性名称(例如http://www.as3commons.org/as3-commons-reflect/index.html

          /安德烈。

          【讨论】:

            【解决方案7】:

            您可以使用 for .. in 循环来获取属性名称,或者使用 for each .. in 循环来获取属性值...

            
            for( var o : * in object){
                trace( o + " = " + object[o] );
            }
            /************* OR ******************/
            for each( var o : * in object ){
                trace( "object has property: " + o );
            }
            

            【讨论】:

              【解决方案8】:

              如果它是一个动态对象,我相信你可以这样做:

              var obj:Object; // I'm assuming this is your object
              
              for(var id:String in obj) {
                var value:Object = obj[id];
              
                trace(id + " = " + value);
              }
              

              这就是它在 AS2 中的做法,我相信这仍然适用于 AS3 中的动态对象。我认为它将显示的属性在非动态对象上更加有限。

              【讨论】:

              • 完美!效果很好。几天来一直试图弄清楚这一点。非常感谢! G人
              • +1 甚至 AS3 也没有 eval 了,这样的事情让它变得动态。
              • “我认为它将显示的属性在非动态对象上更加有限。” for 循环不会通过类的静态定义属性。
              猜你喜欢
              • 2015-03-17
              • 2019-10-17
              • 1970-01-01
              • 1970-01-01
              • 2021-11-16
              • 1970-01-01
              • 2014-02-04
              • 2017-06-29
              • 2010-10-19
              相关资源
              最近更新 更多