James Dawson
James Dawson

Reputation: 5409

Skip first element in "for each" loop?

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!

Upvotes: 12

Views: 25053

Answers (2)

Aviram Fireberger
Aviram Fireberger

Reputation: 4168

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.

Upvotes: 7

d4rklit3
d4rklit3

Reputation: 427

if its an array why not just:

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

}

Upvotes: 12

Related Questions