Dynamically generated Object
I need to dynamically generate a object like this
{type:"typeA",size28:0,size29:0,size30开发者_JS百科:0 etc...}
I get the sizes from a xml file as an array and I need to insert it like this
{type:"typeA",here the generated size array but as the object properties}
How can I do this?
Thanks in advance.
I assume that the length of the array is variable:
var obj : Object = {type:"typeA"}
for (var i : int = 0; i < generatedArray.length; i++) {
obj['size'+(28+i)] = generatedArray[i];
}
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);
精彩评论