简体   繁体   English

遍历字符串数组会产生错误

[英]Iterating over array of strings produces error

I pass in an array of error messages to parse. 我传入了一系列错误消息以进行解析。 An example input would be: 输入示例为:

"An item with this x Id already exists.
 An item with this y id already exists.
 An item with this barcode already exists.
"

That is, the string is literally each line above separated by a \\n, with a final \\n at the end. 也就是说,字符串实际上是上面的每行之间用\\ n分隔,最后是\\ n。

function( msg )
{
  alert( "\"" + msg + "\"" );
  var aLines = msg.split( /\r?\n+/ );

  for ( var i in aLines )
  {
     if ( !aLines[i] ) { alert( "Error!" ); continue; }
     alert( i + ": \"" + aLines[i]  + "\"" );
  }
}

I split it into lines, and iterate over the lines. 我将其分成几行,然后遍历所有行。 At index 3 there is no line and the first conditional triggers. 在索引3处没有行,并且第一个条件触发器。 Should that not be an empty line? 那不是空行吗? eg "" 例如“”

Then the loop actually goes one more element to 4, and shows a the contents of a function. 然后,循环实际上将另一个元素移至4,并显示函数的内容。

That is I get - five alerts: 那就是我得到的-五个警报:

0: "An item with this x Id already exists."
1: "An item with this y id already exists."
2: "An item with this barcode already exists."
Error!

The last one is most bizarre: 最后一个是最奇怪的:

hasObject: "function(o) {
    var l = this.length + 1;
    ... more lines ...
}

I don't understand what is happening here. 我不明白这里发生了什么。 Why is it iterating over one more element? 为什么要迭代一个以上的元素? And why is the last element a function? 为什么最后一个元素是函数? And shouldn't offset 3 be an empty string? 而且偏移量3不应为空字符串吗? That is I should not be alerting "Error!" 那就是我不应该警告“错误!” here. 这里。

As jbabey said, using for .. in loops in Javascript is risky and undeterministic (random order--sometimes). 正如jbabey所说,在Javascript中使用for .. in循环是冒险且不确定的(有时是随机顺序)。 You would most commonly use that for parsing through objects in associative arrays. 您通常会使用它来解析关联数组中的对象。 But, if you insist on keeping the for .. in , wrap the inside of the for with an if block like such: 但是,如果您坚持将for .. in保留在其中for使用if块将for的内部包裹起来,例如:

for (var i in aLines)
{
    if(aLines.hasOwnProperty(i))
    {
        // ... Do stuff here
    }
}

Otherwise, just change it to a classic incremental for loop to get rid of that error: 否则,只需将其更改为经典的增量for循环即可消除该错误:

for (var i = 0; i < aLines.length; i++)
{
   if ( !aLines[i] ) { alert( "Error!" ); continue; }
   alert( i + ": \"" + aLines[i]  + "\"" );
}

You should use a regular for loop for arrays, because for-in will also return all the other property keys in the Array object. 您应该对数组使用常规的for循环,因为for-in还将返回Array对象中的所有其他属性键。 This is why you are seeing "hasObject" (and in my browser, I see a lot more after that): because your array has the function "hasObject", so when you enumerate all of Array's properties this comes up. 这就是为什么您看到“ hasObject”的原因(在我的浏览器中,我看到的更多了):因为您的数组具有函数“ hasObject”,所以当您枚举Array的所有属性时就会出现。

The correct for-loop: 正确的for循环:

  for ( var i = 0, ii = aLines.length; i<ii; i++ )
  {
    if ( !aLines[i] ) { alert( "Error!" ); continue; }
    alert( i + ": \"" + aLines[i]  + "\"" );
  }

Here is your code with the for-in loop replaced with a for loop, and it works as expected: 这是将for-in循环替换为for循环的代码,它可以按预期工作:

http://jsfiddle.net/SamFent/4rzTh/ http://jsfiddle.net/SamFent/4rzTh/

You are getting Error! 您遇到Error! in end because the splitting is giving you empty line "" and when you check for it with if(!aLines[i]) it returns true because it is empty/null/nothing. 最后,因为拆分给您空行""并且当您使用if(!aLines[i])检查它时,它返回true因为它为空/空/无。 You can check it here in a fiddle , you remove the empty line from end and it does not go over array 4 times. 您可以点击此处查看在拨弄 ,您从末的空行,它不会随着阵列走4倍。

I have also put in following code which shows alert: 我还输入了以下显示警报的代码:

    var a="";
    if(!a){
        alert("!a");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM