在javascript中,在特定条件下重复循环的最干净的方法是什么?

在javascript中,在特定条件下重复循环的最干净的方法是什么?,javascript,Javascript,这可以通过常规的for(;;)循环轻松实现,但我喜欢使用for(a/b)循环,我想知道重复迭代的最干净的方式是什么 比如: for (const item of array){ if (something){ //repeat current iteration and don't go further down } //do something } 你可以休息一下 使用do..while和always false条件如何 for (con

这可以通过常规的
for(;;)
循环轻松实现,但我喜欢使用
for(a/b)
循环,我想知道重复迭代的最干净的方式是什么

比如:

for (const item of array){

    if (something){
        //repeat current iteration and don't go further down
    }    

    //do something

}
你可以休息一下


使用
do..while
和always false条件如何

for (const item of [1, 2, 3, 4]) {
  do {
    const result = processItem(item);

    // will call processItem again for the same item if someCondition returns
    // truthy. Otherwise will start processing next item from the array
    if (someCondition(result)) continue;
  } while (0);
}
试试这个:

restartTheLoop:
while (true) {
  for (const item of array){
     if (somethingHappend){
      continue restartTheLoop; //start all over again
     }
     //do stuff
   }

   break;
}
如果somethingHappend为true,那么continue restartLoop将在while循环的下一次迭代中转到continue语句。然后,它会像您希望的那样从一开始就立即启动for循环。
如果for迭代完成(没有什么东西发生,evalute为true),break语句将脱离包含while的循环。

。@NinaScholz您仔细阅读了吗?重复迭代,不要跳到下一个。@yegorchik这将创建一个无限循环。您只想重复一次吗?@JayHales我想在某些条件下重复(如示例中)。假设我迭代了
[1,2,3]
。每次我向服务器执行请求时。一旦我得到一定的响应,我想重复迭代。所以它可以是:1,2,2,3,等等。如果需要的话,它可以重复一次或者100次。类似于不允许
i
for(;;)
循环中递增。但是在的中,这是一个异步循环,所以没有压力让它无限重复。我可以跟踪它,并在需要时使其中断,或者继续到下一个迭代。我希望能够在
for of
循环中使用一些干净的语法执行此操作。这可能是一个解决方案,谢谢。我很高兴看到更多的方法!在while循环结束后,你如何认识到“不要再往下走?”@ThomasSablik,你可以添加另一个条件并继续外循环。问题似乎不清楚,我们解释
//重复当前迭代,不要以不同的方式往下走。对我来说,这意味着在你进入while循环之后,不要做任何事情。js为程序流提供了各种语句。
restartTheLoop:
while (true) {
  for (const item of array){
     if (somethingHappend){
      continue restartTheLoop; //start all over again
     }
     //do stuff
   }

   break;
}