Najlepszy sposób na oderwanie się od zagnieżdżonych pętli w Javascript?

Jaki jest najlepszy sposób na przerwanie zagnieżdżonych pętli w Javascript?

//Write the links to the page.
for (var x = 0; x < Args.length; x++)
{
   for (var Heading in Navigation.Headings)
   {
      for (var Item in Navigation.Headings[Heading])
      {
         if (Args[x] == Navigation.Headings[Heading][Item].Name)
         {
            document.write("<a href=\"" 
               + Navigation.Headings[Heading][Item].URL + "\">" 
               + Navigation.Headings[Heading][Item].Name + "</a> : ");
            break; // <---HERE, I need to break out of two loops.
         }
      }
   }
}
Author: webeno, 2008-10-08

13 answers

Podobnie jak Perl,

loop1:
    for (var i in set1) {
loop2:
        for (var j in set2) {
loop3:
            for (var k in set3) {
                break loop2;  // breaks out of loop3 and loop2
            }
        }
    }

Zgodnie z definicją w EMCA-262 sekcja 12.12. [MDN Docs]

W Przeciwieństwie Do C, te etykiety mogą być używane tylko do continue oraz break, ponieważ Javascript nie posiada goto.

 750
Author: ephemient,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-04 21:19:18

Zawiń to w funkcję, a następnie po prostu return.

 128
Author: swilliams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-05 07:41:22

Jestem trochę spóźniony na imprezę, ale poniżej znajduje się podejście agnostyczne, które nie używa GOTO/etykiet ani owijania funkcji:

for (var x = Set1.length; x > 0; x--)
{
   for (var y = Set2.length; y > 0; y--)
   {
      for (var z = Set3.length; z > 0; z--)
      {
          z = y = -1; // terminates second loop
          // z = y = x = -1; // terminate first loop
      }
   }
}

Na plusie płynie naturalnie, co powinno zadowolić tłum nie-GOTO. Z drugiej strony, pętla wewnętrzna musi zakończyć bieżącą iterację przed zakończeniem, więc może nie być stosowana w niektórych scenariuszach.

 67
Author: aleemb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-07 22:11:59

Zdaję sobie sprawę, że to naprawdę stary temat, ale ponieważ moje standardowe podejście nie jest jeszcze tutaj, pomyślałem, że opublikuję go dla przyszłych googlerów.

var a, b, abort = false;
for (a = 0; a < 10 && !abort; a++) {
    for (b = 0; b < 10 && !abort; b++) {
        if (condition) {
            doSomeThing();
            abort = true;
        }
    }
}
 59
Author: zord,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-18 20:08:16
var str = "";
for (var x = 0; x < 3; x++) {
    (function() {  // here's an anonymous function
        for (var y = 0; y < 3; y++) {
            for (var z = 0; z < 3; z++) {
                // you have access to 'x' because of closures
                str += "x=" + x + "  y=" + y + "  z=" + z + "<br />";
                if (x == z && z == 2) {
                    return;
                }
            }
        }
    })();  // here, you execute your anonymous function
}
Jak to? :)
 35
Author: harley.333,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-08 19:42:23

Dość proste

var a=[1,2,3];
var b=[4,5,6];
var breakCheck1=false;

for (var i in a){
    for (var j in b){
        breakCheck1=true;
        break;
    }
    if (breakCheck1) {break;}
}
 32
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-16 15:22:31

Jak o użyciu żadnych przerw w ogóle, żadnych flag przerwania, i żadnych dodatkowych kontroli stanu. Ta wersja po prostu usuwa zmienne pętli (czyni je Number.MAX_VALUE), gdy warunek jest spełniony i wymusza eleganckie zakończenie wszystkich pętli.

// No breaks needed
for (var i = 0; i < 10; i++) {
  for (var j = 0; j < 10; j++) {
    if (condition) {
      console.log("condition met");
      i = j = Number.MAX_VALUE; // Blast the loop variables
    }
  }
}

Była podobna odpowiedź dla pętli zagnieżdżonych, ale działa to dla pętli zagnieżdżonych bez konieczności rozważania wartości zakończenia każdej pętli dla prostych pętli.

Inny przykład:

// No breaks needed
for (var i = 0; i < 89; i++) {
  for (var j = 0; j < 1002; j++) {
    for (var k = 0; k < 16; k++) {
      for (var l = 0; l < 2382; l++) {
        if (condition) {
          console.log("condition met");
          i = j = k = l = Number.MAX_VALUE; // Blast the loop variables
        }
      }
    }
  }
}
 12
Author: Drakes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-11 01:33:57

A może przepchnięcie pętli do ich granic końcowych

    for(var a=0; a<data_a.length; a++){
       for(var b=0; b<data_b.length; b++){
           for(var c=0; c<data_c.length; c++){
              for(var d=0; d<data_d.length; d++){
                 a =  data_a.length;
                 b =  data_b.length;
                 c =  data_b.length;
                 d =  data_d.length;
            }
         }
       }
     }
 4
Author: user889030,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-26 10:58:40

Jeśli używasz Coffeescript, istnieje wygodne słowo kluczowe "do", które ułatwia zdefiniowanie i natychmiastowe wykonanie funkcji anonimowej:

do ->
  for a in first_loop
    for b in second_loop
      if condition(...)
        return

... więc możesz po prostu użyć "return", aby wydostać się z pętli.

 3
Author: Nick Perkins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-10 20:46:35

Pomyślałem, że pokażę podejście do programowania funkcjonalnego. Możesz wyłamać się z zagnieżdżonej tablicy.prototyp.some() i / lub Array.prototyp.every () functions, as in my solutions. Dodatkową zaletą tego podejścia jest to, że Object.keys() wylicza tylko własne właściwości obiektu, podczas gdy "pętla for-in wylicza również właściwości w łańcuchu prototypów" .

Blisko rozwiązania OP:

    Args.forEach(function (arg) {
        // This guard is not necessary,
        // since writing an empty string to document would not change it.
        if (!getAnchorTag(arg))
            return;

        document.write(getAnchorTag(arg));
    });

    function getAnchorTag (name) {
        var res = '';

        Object.keys(Navigation.Headings).some(function (Heading) {
            return Object.keys(Navigation.Headings[Heading]).some(function (Item) {
                if (name == Navigation.Headings[Heading][Item].Name) {
                    res = ("<a href=\""
                                 + Navigation.Headings[Heading][Item].URL + "\">"
                                 + Navigation.Headings[Heading][Item].Name + "</a> : ");
                    return true;
                }
            });
        });

        return res;
    }

Rozwiązanie redukujące iterację nad nagłówkami/przedmiotami:

    var remainingArgs = Args.slice(0);

    Object.keys(Navigation.Headings).some(function (Heading) {
        return Object.keys(Navigation.Headings[Heading]).some(function (Item) {
            var i = remainingArgs.indexOf(Navigation.Headings[Heading][Item].Name);

            if (i === -1)
                return;

            document.write("<a href=\""
                                         + Navigation.Headings[Heading][Item].URL + "\">"
                                         + Navigation.Headings[Heading][Item].Name + "</a> : ");
            remainingArgs.splice(i, 1);

            if (remainingArgs.length === 0)
                return true;
            }
        });
    });
 2
Author: Zachary Ryan Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-24 18:36:27
XXX.Validation = function() {
    var ok = false;
loop:
    do {
        for (...) {
            while (...) {
                if (...) {
                    break loop; // Exist the outermost do-while loop
                }
                if (...) {
                    continue; // skips current iteration in the while loop
                }
            }
        }
        if (...) {
            break loop;
        }
        if (...) {
            break loop;
        }
        if (...) {
            break loop;
        }
        if (...) {
            break loop;
        }
        ok = true;
        break;
    } while(true);
    CleanupAndCallbackBeforeReturning(ok);
    return ok;
};
 -3
Author: Triqui,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-12 22:16:16

The best way is -
1) posortuj obie tablice, które są używane w pierwszej i drugiej pętli.
2) Jeśli element jest dopasowany, przerwij wewnętrzną pętlę i przytrzymaj wartość indeksu.
3) Po uruchomieniu następnej iteracji Uruchom wewnętrzną pętlę z wartością indeksu hold.

 -4
Author: Deepak Karma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-23 17:59:58

Wiem, że pytano o to 8 lat temu, ale w ES6 dostaliśmy za...pętli , która umożliwia korzystanie z funkcji Standard break :

for (let item of items) {
  if (item.id === id) {
    //do something cool
    break;
  }
}
 -5
Author: vladexologija,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-27 14:02:48