Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 350
James Dawson Avatar asked Mar 28 '12 22:03

James Dawson


People also ask

How do you skip first in for loop?

Use iter() and next() to skip first element of a for-loop User iter(object) to return an iterable for any iterable object . Call next(iterator) on iterator as the iterable for object to skip the first element of iterator .

How do you skip the first element of an array?

The shift() method removes the first element from an array and returns that removed element.


2 Answers

if its an array why not just:

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

}
like image 185
d4rklit3 Avatar answered Oct 20 '22 20:10

d4rklit3


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.

like image 39
Aviram Fireberger Avatar answered Oct 20 '22 21:10

Aviram Fireberger