9

I have an array of sprites called segments and I would like to skip the first element of segments in my for each loop. I'm doing this at the moment:

var first = true;
for each (var segment in this.segments)
{
    if(!first)
    {
        // do stuff
    }

    first == false;
}

Is there a better way to do this? Thanks!

James Dawson
  • 5,309
  • 20
  • 72
  • 126
  • Does ActionScript "fix" using `for..in` over Arrays? In JavaScript `for(i = 0; i <= arr.length; i++) { doStuff(arr[i]) }` is the *correct way* to iterate over an Array and should be used instead... in which case skipping the first element is trivial (start at `i = 1`). –  Mar 28 '12 at 23:02
  • If you don't want to process each array entry, don't use `for each`. `For each` specifically means "for each", just like it says. Read the words literally, and if you don't want to process every item, use a standard `for` loop instead of `for each`. – Ken White Mar 28 '12 at 23:44

2 Answers2

11

if its an array why not just:

for(var i:int = 1; i < this.segments.length; i++)
{

}
d4rklit3
  • 417
  • 1
  • 5
  • 12
  • Well I don't know what the ultimate goal of this was. I figured if you want to pull out items from an array in order you would use for loop anyways – d4rklit3 Mar 29 '12 at 03:26
  • **Swift 5** raises error: "`C-style for statement has been removed in Swift 3`" – Top-Master Dec 22 '21 at 06:01
5

This can also be done via "slice". For example

for (var segment in this.segments.slice(1))
{
    
}

Array#slice will copy the array without the first element.

Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69