Flex中是否存在使用点分隔字符串获取对象嵌套属性的实用程序:
SomeUtil.getObjectProperty(object, "child.property");
答案 0 :(得分:3)
假设对象是动态对象,您可以自己滚动:
public function getObjectProperty(object:Object, property:String):Object
{
var parts:Array = property.split(".");
if(parts && parts.length == 2 && object && object[parts[0]] && object[parts[0]][parts[1]])
{
return object[parts[0]][parts[1]];
}
return null;
}
这是另一个可以使用不同字符串的方法:
private function test(e:Event = null):void
{
var obj:Object = {
"child": {
"property":1
},
"anotherproperty": 2
};
var test1:Object = getObjectProperty(obj, "anotherproperty");
var test2:Object = getObjectProperty(obj, "child.property");
}
public function getObjectProperty(object:Object, property:String):Object
{
var parts:Array = property.split(".");
var returnProp:Object = null;
for (var i:int = 0; i < parts.length; i++)
{
if(object[parts[i]])
{
returnProp = object[parts[i]];
object = returnProp;
}
else
return null;
}
return returnProp;
}