我需要动态生成像这样的对象
{type:"typeA",size28:0,size29:0,size30:0 etc...}
我从xml文件中获取大小作为数组,我需要像这样插入
{type:"typeA",here the generated size array but as the object properties}
我该怎么做?
提前致谢。
答案 0 :(得分:1)
我假设数组的长度是可变的:
var obj : Object = {type:"typeA"}
for (var i : int = 0; i < generatedArray.length; i++) {
obj['size'+(28+i)] = generatedArray[i];
}
答案 1 :(得分:1)
var xmlData:XML = <obj>
<contents>size28:5,size29:3,size30:9</contents>
</obj>;
var obj:Object = new Object();
var xmlObjArray:Array = xmlData.contents.split(",");
for (var i in xmlObjArray)
{
var objProp:Array = xmlObjArray[i].split(":");
obj[objProp[0]] = objProp[1];
}
trace (obj.size29);