【问题标题】:Skipping multiple elements in a FOR loop, Javascript在 FOR 循环中跳过多个元素,Javascript
【发布时间】:2015-07-17 12:41:02
【问题描述】:

我有一些文件内容想使用 javascript 函数传递到我的服务器。在文件中,有许多空行,或者包含无用文本的行。到目前为止,我将每一行作为存储在数组中的字符串读取。

然后我如何遍历该内容以跳过多行,例如第 24、25、36、42、125 行等。我可以将这些元素 id 放入一个数组中并告诉我的 for 循环在除这些之外的每个元素上运行吗?

谢谢

【问题讨论】:

  • 您可以为此目的使用 continue 语句。如果你愿意,我可以发布一个例子
  • 对于跳过loops你的云使用continue。这些数字也是随机的。或针对不同的文件进行修复。

标签: javascript arrays for-loop skip


【解决方案1】:

如果您有许多索引要跳过,这取决于数组的元素,您可以编写一个函数,返回该数组中每个索引要跳过的元素数(如果不需要跳过,则返回 1 ):

for ( let i = 0; 
      i < array.length;
      i += calcNumberOfIndicesToSkip( array, i )){
   // do stuff to the elements that aren't 
   // automatically skipped
}

function calcNumberOfIndicesToSkip( array, i ){
  // logic to determine number of elements to skip
  // (this may be irregular)
  return numberOfElementsToSkip ;
}

在你的情况下:

                               // skip the next index (i+1)?
for ( let i=0; i<array.length; i+=skipThisIndex(i+1) ){ 
    // do stuff 
}

function skipThisIndex(i){
    const indicesToSkip = [ 24, 25, 36, 42, 125 ];
    return 1 + indicesToSkip.includes(i);
}

// returns 1 if i is not within indicesToSkip 
// (there will be no skipping)
//      => (equivalent to i++; normal iteration)
// or returns 1 + true (ie: 2) if i is in indicesToSkip
//      => (index will be skipped)

【讨论】:

    【解决方案2】:

    你可以试试这个,它不是很优雅,但会很成功

    <html>
    <body>
    
    <p>A loop which will skip the step where i = 3,4,6,9.</p>
    
    <p id="demo"></p>
    
    <script>
    var text = "";
    var num = [3,4,6,9];
    var i;
    for (i = 0; i < 10; i++) {
    var a = num.indexOf(i);
    if (a>=0) { 
    continue; 
    }
    text += "The number is " + i + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
    </script>
    
    </body>
    

    【讨论】:

    • 粗鲁但有效
    【解决方案3】:

    另一种方法:

    var lines = fileContents.match(/[^\r\n]+/g).filter(function(str,index,arr){
        return !(str=="") && uselessLines.indexOf(index+1)<0;
    });
    

    【讨论】:

      【解决方案4】:

      你可以使用这样的东西

          var i = 0, len = array1.length;
          for (; i < len; i++) {
              if (i == 24 || i == 25) {
                  array1.splice(i, 1);
              }
          }
      

      或者你可以有另一个数组变量来获取所有需要从array1中删除的项目

      【讨论】:

        【解决方案5】:

        你不能告诉你的 for 循环迭代所有,而是跳过某些元素。它基本上只会在任何方向(简化)计算,直到满足某个标准。

        但是,您可以在循环中放置一个 if 来检查某些条件,如果满足条件,则选择不执行任何操作。例如:

        (下面是伪代码,谨防打字错误)

        for(var line=0; line < fileContents.length; line++) { 
            if(isUselessLine(line)) { 
                continue; 
            }
            // process that line
        }
        

        continue 关键字基本上告诉 for 循环“跳过”当前迭代的其余部分并继续下一个值。

        isUselessLine 函数是你必须自己实现的东西,在某种程度上,如果给定行号的行对你无用,它会返回 true。

        【讨论】:

        • 谢谢,是的,我希望可能有一个更优雅的解决方案,但这很好用。我现在正在使用 Arrays.asList(uselesslines).contains()) 进行检查。
        猜你喜欢
        • 1970-01-01
        • 2014-10-19
        • 2014-01-08
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多