跳过FOR循环中的多个元素,Javascript [英] Skipping multiple elements in a FOR loop, Javascript

查看:123
本文介绍了跳过FOR循环中的多个元素,Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some file contents I'd like to pass on to my server using a javascript function. In the file, there are many empty lines, or those which contain useless text. So far I read every line in as a string stored in an array.

然后我该如何跳过该行,从而跳过多行,例如行24、25、36、42、125等.我可以将这些元素ID放入数组中并告诉我的for循环在除这些元素之外的所有元素上运行吗?

How do I then loop through that content skipping multiple lines such as lines 24,25, 36, 42, 125 etc. Can I put these element id's into an array and tell my for loop to run on every element except these?

谢谢

推荐答案

您不能让for循环重复所有操作,而是跳过某些元素.它基本上只会在任何方向上(简化)计数,直到满足特定条件为止.

you can't tell your for loop to iterate all, but skip certain elements. it will basically just count in any direction (simplified) until a certain critera has been met.

但是,您可以将if放入循环中以检查某些条件,如果满足条件,则不执行任何操作.例如:

you can however put an if inside your loop to check for certain conditions, and chose to do nothing, if the condition is met. e.g.:

(下面的伪代码,请小心输入错误)

(pseudo code below, beware of typing errors)

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

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

the continue keyword basically tells the for loop to "jump over" the rest of the current iteration and continue with the next value.

如果具有给定行号的行对您无用,则isUselessLine函数必须以某种方式自己实现,该函数将返回true.

The isUselessLine function is something you'll have to implement yourself, in a way, that it returns true, if the line with the given linenumber is useless for you.

这篇关于跳过FOR循环中的多个元素,Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆