1

DISCLAIMER: relatively new to Flex/AS3, I might be missing something obvious.

After doing some research it appears that using the for(var property:String in object) does not guarantee the enumeration order of properties, however it doesn't say anything about modifying the VALUE that property points to as changing the for...in loop. See For-Each Loop AS3: Is the direction guaranteed? for background info.

My example is as follows:

for(var i:int = 0; i<objectArray.length; i++)
{
    for(var property:String in objectArray[i])
    {
        objectArray[i][property] = unescape(objectArray[i][property]);
    }
}

objectArray[i] has properties a, b, c, d, e, f. When I step through the code here I get c, b, d, c, a, f. Notice that I get c twice.

As a solution I can create a temp object to store the data while doing the loop on the original object, and then replace the original object with the modified data, but I'd like to know what exactly is going on.

So my question is, does the value of object property modify the looping order of the for...in construct? And if so, should it or is this a bug?

4
  • That's very bizarre. Are you positive 'c' is the same property or is it just that two of your properties have the same value? Commented Dec 9, 2009 at 14:57
  • Yes, each property is unique. The reason this problem came up is that in certain cases it skips one of the properties that actually has to have the value unescaped or the display is incorrect. Commented Dec 9, 2009 at 15:02
  • As a quick test, trace the value of each property in the array (without altering it, i.e. comment out the current line) and see if it hits it twice. If it doesn't, I'm inclined to go with AndreaG and state "use a copy if you're going to change something." It's possible that modifying it does something to the collection. Commented Dec 9, 2009 at 15:07
  • With tracing only, the loop functions as expected. Still a little odd that the loop appears to be based not only on the property, but also the value that the property points to. Commented Dec 9, 2009 at 15:18

3 Answers 3

2

Altering the contents of a variable you are looping over is rarely a good thing to do. In Java for example you can't iterate over a thing you are modifying.

You should produce a copy, work on the copy and after the loop is over re-assign the new data to the variable in a single instruction.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, creating a copy and changing that does work (that was my solution as well). But does it really make sense that when iterating over properties of an object that changing the value that property points to, would changed the iteration order?
I think it does. That is, since you are doing a bad thing (changing things while you are reading them), nothing is guaranteed anymore.
1

Your assignments are messing with the iterator order, what to you looks like just the reassignment of a member on an object could be something quite different behind the scenes. I'm guessing the object you are iterating over is not of a sealed class?

As AndreaG mentions you could make your changes on a copy, there is another option if you don't want to change the reference to your original array but don't mind iterating twice.

First iterate over the array to create a (new) array of keys.

Then, iterate over the array of keys and update the values on the original object.

1 Comment

So we are all agreed about what is happening, and a workaround for it. Hopefully this can help someone else in the future if they have the same issue. My new question is, does this behavior make sense? Or is this an implementation bug on their part?
0

i guess, this choice was made for performance ...

to ensure, that altering the underlying structure doesn't have any effect on iteration would require to extract all keys into a list first, and then iterate over that list ... that makes 1 extra iteration, and also, the first might be unneccessary, if for example you're doing a search operation, which means you'll probably cancel iteration at some point ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.