1
votes

It's me again and I'm really having troubles with that simple thing -.-

I have an array with 5 numbers in it and I want for each number to create an own variable

For instance:

Array [4, 12, 52, 23, 55]. create:
Variable1 = 4;
Variable2 = 4;

Now, I know that I can do that with a For Loop but however I simply dont know how to attach a NUMBER to the variable's name which I'm defining. I have something like this already:

                    var number:int=0;

                for (var bla in xvalue) {
                    var endxvalue+nummer:int = +xvalue[bla];
                    number++;
                }

So that I get the varibables endxvalue1 = ..; endxvalue2= ...; etc. but i cannot get the numbers after the variables name (endvalue*1*, endvalue*2*, etc) handled.

How can I achieve that? Thanks!

2

2 Answers

0
votes

As far as I know, dynamic names for local variables are not allowed. But! You can use Object as a collection to handle your task.

var xvalue:Array = [4, 12, 52, 23, 55]; 

var endvalues:Object = new Object();
for each (var i:int in xvalue)
{
    endvalues['endxvalue' + i] = i;
}

trace(endvalues.endxvalue4); //traces "4"
0
votes

A short answer: use this[] with a string expression in brackets to create a custom property.

for (k in yourArray) {
    v=yourArray[k];
    this['Variable'+k]=v;
}

But, it'll be WAY better to use the array as it is, addressing as this.yourArray[index].