Javascript 基于两个条件退出 While 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12397779/
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-08-23 07:53:39  来源:igfitidea点击:

Exit a While loop based on two conditions

javascript

提问by user3357963

I have a question relating to the answer on this post Javascript code to parse CSV data

我有一个关于这篇文章的答案的问题Javascript code to parse CSV data

I'm finding I get an extra "\r\n" on the end which I don't want to add to the array. I've tried to break out of the while loop...

我发现最后有一个额外的“\r\n”,我不想将其添加到数组中。我试图摆脱 while 循环......

The original working line is

原来的工作线是

 while (arrMatches = objPattern.exec( strData )){

but I need to break out if arrMatches = "\r\n"

但如果 arrMatches = "\r\n" 我需要爆发

while ((arrMatches[ 1 ] != "\r\n") && arrMatches = objPattern.exec( strData )){

but get an Invalid left-hand side in assignmenterror.

但得到一个Invalid left-hand side in assignment错误。

What is the correct syntax?

什么是正确的语法?

采纳答案by dbf

This approach should work, the only thing is that arrMatches should be between ( )too, to avoid arrMatchesbeing set to true from the second condition.

这种方法应该有效,唯一的问题是 arrMatches 也应该介于两者之间( ),以避免arrMatches从第二个条件设置为 true。

while ((arrMatches = objPattern.exec( strData )) && (arrMatches[ 1 ] != "\r\n")) {

回答by zeacuss

just separate the two conditions to make it more readable and understandable

只需将两个条件分开,使其更具可读性和可理解性

while(arrMatches = objPattern.exec( strData )){

    if(arrMatches[ 1 ] == "\r\n"){
        break;
    }
    /*
     *if(arrMatches[ 1 ] == "\r\n")
     *   break;
     */
     // rest of code
}

回答by Arman McHitarian

Another way: the expression block of the whileoperator can be easily split up into a chain of comma-separated expressions expecting the loop to break once the last expression evaluates to 0/false.

另一种方式:while运算符的表达式块可以很容易地分成一串逗号分隔的表达式,一旦最后一个表达式的计算结果为 0/false,循环就会中断。

It's NOT equivalent to logical && chainingsince a comma ','operators in JS always return the last expression. (Thanks GitaarLab to remind me about that)

它不等同于逻辑 && 链接,因为','JS 中的逗号运算符总是返回最后一个表达式。(感谢 GitaarLab 提醒我这一点

In these examples the loop stops once the last variable reaches 0 and thus evaluates to false.

在这些示例中,一旦最后一个变量达到 0,循环就会停止,因此计算结果为 false。

var i = 10, j = 10;
while (i--, j--) console.log(i);
/*9
8
7
6
5
4
3
2
1
0*/

var i = 10, j = 5;
while (i--, j--) console.log(i);
/*9
8
7
6
5*/

var i = 10, j = 5, k = 3;
while (i--, j--, k--) console.log(i);
/*9
8
7*/

回答by aug

You could try a whileloop that handles one condition, and inside the whileloop, you have an ifstatement that checks the other condition.

您可以尝试while处理一个条件的while循环,并且在循环内部,您有一个if检查另一个条件的语句。

Example:

例子:

while (one condition) {
   if (other condition) {
       do something;
   }
}

Whether this is the appropriate way to do it, I'm not entirely sure. I'll update my answer if I find something better.

这是否是适当的方法,我不完全确定。如果我找到更好的答案,我会更新我的答案。

回答by lleite

Try: while ((arrMatches[ 1 ] != "\r\n") && arrMatches == objPattern.exec( strData )){

尝试: while ((arrMatches[ 1 ] != "\r\n") && arrMatches == objPattern.exec( strData )){

With a single '=', you are actually assigning a value to arrMatches. In order to compare values you should use ==.

使用单个 '=',您实际上是在为arrMatches分配一个值。为了比较值,您应该使用==