How to dynamically create AS3 Object properties?

Object is not MovieClip object is object what I am trying to do is provide some data to my Flex chart and it depends from user how many data he will provide.

So I have a for loop, I want it to work like this.

var obj:Object = {};
for(var i:int = 0; i < _someArray.length; i++){
   obj["someProperty" + i] = _someArray[i];
// and so on
}

// then I want to read those the same way for example
for(var i:int = 0; i < _someArray.length; i++){
   trace(obj["someProperty" + i]);
}

this doesnt work for me when I debug the obj is empty it doesnt have any properties or values…





That works perfectly for me. I cut-and-pasted it exactly as it is written (but removing the duplicate definition for “i”) into a new AS3 FLA, added :

var _someArray:Array = ["one", "two" ,"three", "four"];

at the top of the code, and it traces :

one

two

three

four

hmm so maybe it traces out but it doesnt actually crate the properties in the object, thats why It doesnt work as data provider…

Now I have no idea what you mean.
How can it not be creating the properties on the object if the second loop tracing the value of those properties, on that object?

hmm so maybe it traces out but it doesnt actually crate the properties in the object, thats why It doesnt work as data provider...

What does it need to do with the ‘traced’ values? For me it also traces ok.

This works every time for me:


var mc:MovieClip = new MovieClip();

mc["theProperty"] = 100;

var shape:Shape = new Shape();
shape.graphics.beginFill(0);
shape.graphics.drawRect(0, 0, 100, 100);
shape.graphics.endFill();

shape.x = shape.y = mc["theProperty"];

addChild(shape);

Assign dynamic property to “dynamic Object” should works well as my experience and it maybe good to trace assigning value before pass it to Object. (It happens lots we notice bug in wrong place)

var obj:Object = {};
for(var i:int = 0; i < _someArray.length; i++){
  trace("i = " + i); //make sure loop does run
  trace("target value = " + _someArray[i]);
  obj["someProperty" + i] = _someArray[i];
  trace("obj value = " + obj["someProperty" + i]);
}