typescript 如何在 Type Script 中为每个循环中断/继续嵌套

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48802066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:52:21  来源:igfitidea点击:

How to break/continue across nested for each loops in Type Script

typescript

提问by gihan-maduranga

I tried to use break inside nested for each loop and it says jump target cannot cross function boundary. please let me know how can i break nested for each loop when certain condition is met in type script.

我尝试在每个循环的嵌套内使用 break ,它说jump target cannot cross function boundary。请让我知道如何在类型脚本中满足某些条件时为每个循环中断嵌套。

groups =[object-A,object-B,object-C]
    groups.forEach(function (group) {
    // names also an array
        group.names.forEach(function (name) {

        if (name == 'SAM'){
         break; //can not use break here it says jump target cannot cross function boundary
      }

    }

    }

回答by Mahmoodvcs

forEachaccepts a function and runs it for every element in the array. You can't break the loop. If you want to exit from a single run of the function, you use return.

forEach接受一个函数并为数组中的每个元素运行它。你不能打破循环。如果您想从函数的单次运行中退出,请使用return.

If you want to be able to break the loop, you have to use for..ofloop:

如果你想能够打破循环,你必须使用for..of循环:

  for(let name of group.names){
    if (name == 'SAM') {
      break;
    }
  }

回答by lazydeveloper

ForEach doesn't support break, you should use return

ForEach 不支持 break,你应该使用 return

  groups =[object-A,object-B,object-C]
        groups.forEach(function (group) {
        // names also an array
            group.names.forEach(function (name) {

            if (name == 'SAM'){
             return; //
          }
     }
   }

回答by Ayoub ELaboussi

Object.keys(fields).forEach(function (key, index) {
  if (fields[key] !== null && fields[key].toString().trim().length === 0) {
    console.log('error');
    return;
  }
});

回答by David Jones

I don't have enough reputation to comment, but when I tried Mahmoodvcs' answer, I got a compile error "Cannot find name "name"".

我没有足够的声誉来发表评论,但是当我尝试 Mahmoodvcs 的回答时,我收到了一个编译错误“找不到名称“名称””。

The solution is to declare your variable ahead of the loop, it won't be instantiated automatically as in a forEach. Simple solution, but you never know what might trip you up, so hopefully it helps somebody.

解决方案是在循环之前声明您的变量,它不会像在 forEach 中那样自动实例化。简单的解决方案,但你永远不知道什么会绊倒你,所以希望它对某人有所帮助。

This snippet is the closest I could get to a forEach syntax with the desired behavior:

这个片段是我能得到的最接近具有所需行为的 forEach 语法:

for(let name of group.names){
  if (name == 'SAM') {
    break;
  }
}

Also, my case was slightly different. I was using a return inside a forEach loop, but I intended the return to apply to the containing function, not the forEach. This does not return an error, so I was lucky I had viewed this post earlier or I might have been banging my head on my desk all day. My code went from:

另外,我的情况略有不同。我在 forEach 循环中使用了 return,但我打算将 return 应用于包含函数,而不是 forEach。这不会返回错误,所以我很幸运我早些时候看过这篇文章,否则我可能整天都在敲我的头。我的代码来自:

 Object.keys(this.ddlForms).forEach(x => {
        if (!(!this.ddlForms[x].filterControl.value || this.ddlForms[x].filterControl.value[0] == 'All' || this.ddlForms[x].filterControl.value.some(y => y == data[this.ddlForms[x].fieldName]))) {//does not meet any of these three conditions
          return false;
        }
      });

to:

到:

for(let x of Object.keys(this.ddlForms)) {
        if (!(!this.ddlForms[x].filterControl.value || this.ddlForms[x].filterControl.value[0] == 'All' || this.ddlForms[x].filterControl.value.some(y => y == data[this.ddlForms[x].fieldName]))) {//does not meet any of these three conditions
          return false;
        }
      }

回答by InspiredDeveloper

ForEach doesn't support break nor return. It says it supports return for forEach but it doesn't do anything in real. Here is the article that explains you clearly about this. If you use ".some" then it does return for sure once it finds the first item. You can use .some only if you want to return from loop as soon as you find the first item. If you want to do some operation on each element then use .foreach function.

ForEach 不支持 break 或 return。它说它支持 forEach 的 return 但它实际上没有做任何事情。这篇文章清楚地向您解释了这一点。如果您使用“.some”,那么一旦找到第一个项目,它确实会返回。仅当您想在找到第一项后立即从循环返回时才可以使用 .some。如果你想对每个元素做一些操作,那么使用 .foreach 函数。

https://www.knowledgescoops.com/2019/07/javascript-array-some-vs-every-vs_27.html

https://www.knowledgescoops.com/2019/07/javascript-array-some-vs-every-vs_27.html

回答by Ryan D

If you're just interested in knowing if "Sam" is or is not in the list, consider using the .somemethod

如果您只是想知道“Sam”是否在列表中,请考虑使用该.some方法

if(group.names.some(name => name == 'SAM'))
    // Do logic here.