Veltar
Veltar

Reputation: 741

leave a for each loop

I want to leave the for each loop as soon as the boolean is set to true.

for each (xmlEvent in arEvents)
{
    var xmlEvent:XMLList = XMLList(event);
    if(xmlEvent.id == artistEvent.id)
    {
        boolExists = true;
    }
}

I've tried break and such, but this all doesn't work.

Upvotes: 2

Views: 2634

Answers (2)

merv
merv

Reputation: 76700

Why not use the some() method? It'll handle all that for you.

boolExists = arEvents.some(
  function (xmlEvent:*, index:int, arr:Array):Boolean {
    return xmlEvent.id == artistEvent.id;
  });

Note: The anonymous function is used only for brevity.


For clarity, the some() method only iterates over the array until the first item that returns true is reached, at which point iteration is terminated and the result (true) is returned.

Upvotes: 2

Timofei Davydik
Timofei Davydik

Reputation: 7294

Use break operator

for each (xmlEvent in arEvents)
{
    var xmlEvent:XMLList = XMLList(event);
    if(xmlEvent.id == artistEvent.id)
    {
        boolExists = true;
        break;
    }
}

Upvotes: 8

Related Questions