views:

41

answers:

2

I need to dynamically generate a object like this

{type:"typeA",size28:0,size29:0,size30: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.

+1  A: 

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];
}
Daniel Engmann
+1. Correct solution. Although I don't really understand why having properties `name1` to `name247` is better than an array :D
back2dos
I use this Object as the row of a datagrid in flex. My ArrayCollection consists of multiple object like this.
chchrist
+1  A: 
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);
dome