David Wilson
David Wilson

Reputation: 15

for each not updating variables

When using the AS3 for each construct I find that I can't update the members of the array that im iterating.

for each( obj:Object in array ){
    obj = new Object();
}

when I loop over the array again they will still have the same values as before the loop.

Am I stuck with using tradition for-loops in this situation or is there a way to make the updates stick.

Upvotes: 0

Views: 49

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

As Daniel indicated, you are instantiating a new object to the obj reference instead of the array element. Instead, access the array by ordinal:

var array:Array = [{}, {}, {}];

for (var i:uint = 0; i < array.length; i++)
{
    array[i] = {};
}

Upvotes: 1

Related Questions