问题
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
- 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?
回答1:
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.
回答2:
flash.utils.describeType(value:*) will also give you a list of properties on an object.
回答3:
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
回答4:
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.
回答5:
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
回答6:
// 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;
}
回答7:
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 );
}
回答8:
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
}
来源:https://stackoverflow.com/questions/372317/how-can-i-get-list-of-properties-in-an-object-in-actionscript