How to dynamically create AS3 Object properties?

For example:

 _obj["property" + _var] = "VALUE";

I need to dynamically create properties for example _var is a Number and I want to create in a loop property0, property1 and so on in my object how can I achieve this?

Something like this?

var obj:Object = {};

var i:int = 0
for(i; i< 5; i++){
  obj["property" + i] = i
}

for(var id:String in obj) {
  trace(id + " = " + obj[id]);
}

Yeah, but it doesnt work for me, I cannot read the value like this:

trace(_obj["property1"]);
??
trace(_obj.property1);
trace(_obj.property1);

yeah I know that but the property is crated dynamically so I want to do something like:

trace(_obj["property"+_var]);

maybe trace(_var) first? maybe it is wrong value :slight_smile:

this works for me.

var obj:Object = {};
var n:uint = 1;
obj['t' + n] = 100;
trace(obj["t" + n]);
maybe trace(_var) first? maybe it is wrong value :)

this works for me.

var obj:Object = {};
var n:uint = 1;
obj['t' + n] = 100;
trace(obj["t" + n]);

hmmm strange, because the _var for sure has the right value ;D

Does your class extend a dynamic class like MovieClip?

You can do it with associative arrays as well:

var dataArr:Array = new Array();
 
 var i:int = 0
 for(i; i< 5; i++){
dataArr.push( { prop: 'property' + i, val: i} );
 }
 
 i=0
 for(i; i< 5; i++){
 trace( dataArr[i].prop + ' ' + dataArr[i].val  );
 }
You can do it with associative arrays as well:
var dataArr:Array = new Array();
 
 var i:int = 0
 for(i; i< 5; i++){
dataArr.push( { prop: 'property' + i, val: i} );
 }
 
 i=0
 for(i; i< 5; i++){
 trace( dataArr[i].prop + ' ' + dataArr[i].val  );
 }

but I need to have it like name:vlaue and not propert:name_of_property, value:value_of_property this is data provider for Flex Chart which is Array Collection based :slight_smile:

this works for me

var _obj:Object = new Object();
var _var:int =1;

var i:int = 0;
for(i; i< 5; i++){
  _obj["property" + i] = i;
}

trace(_obj["property"+_var]);

are you initializing the Object instance? cause thats the only way it doesn’t work

Since we are talking about dynamic stuff, I’m finding this very useful if the function name is not yet known (trace could be anything, for example via XML).

var currFunc:Function = trace;
currFunc("ee")

1K (1024) posts, what a nice rounded number :wink:

hmmm still can’t understand what you are trying to do

Tean 's code should work for you

do you mean …:

 _obj["property" + 1] = "VALUE";
 

then you want … _obj.property1 ?

If so , this _obj should be dynamic , as P&K said if the _obj is a MovieClip extended Object or other

dynamic public class Obj extends MovieClip

public dynamic class Obj extends MovieClip

@ Firsh congrts :smiley:

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…

Try using var k instead of i at the second loop. Define the _someArray.length as a variable outside the loops.

Try using var k instead of i at the second loop. Define the _someArray.length as a variable outside the loop.

lol it is not the fault of the loop I can write it without the loop and it doesnt work either ;D

Try using var k instead of i at the second loop. Define the _someArray.length as a variable outside the loop.

lol it is not the fault of the loop I can write it without the loop and it doesnt work either ;D

Just trying to figure out what we are doing differently.

var obj:Object = new Object();
var _someArray:Array = new Array("one","two","three");
var len:int = _someArray.length;
for(var i:int = 0; i < len; i++){
   obj["someProperty" + i] = _someArray[i];
// and so on
}

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

This outputs one two three (working). Even if it is inside a class like this:

...
	public class Dyn extends Sprite {
		public function Dyn() {
...
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

It works for me:

var _someArray:Array = ["one","two", new Sprite()]
			var obj:Object = {};
			for (var i:int = 0; i < _someArray.length; i++) {
			   obj[("someProperty" + i)] = _someArray[i];
			}
			for (var name:String in obj) {
				trace("loop init -> "+ name + " -> " + obj[name] )
			}
			// then I want to read those the same way for example
			for(i = 0; i < _someArray.length; i++){
			   trace(obj[("someProperty" + i)]);
			}

Traces:

loop init -> someProperty0 -> one
loop init -> someProperty1 -> two
loop init -> someProperty2 -> [object Sprite]
one
two
[object Sprite]

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…