When mutating a variable inside a for-loop, these changes get reverted on loop exit.
The presence of a test for this scenario suggests this is by design, but it is quite unlike most programming languages. Unfortunately this makes you use more boilerplate code inside templates than necessary.