一尘不染

打破JavaScript中的嵌套循环的最佳方法是什么?

javascript

打破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.
         }
      }
   }
}

阅读 231

收藏
2020-05-01

共1个答案

一尘不染

就像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
            }
        }
    }

如EMCA-262第12.12节所定义。[MDN文件]

与C不同,这些标签只能用于continuebreak,而Javascript没有goto

2020-05-01