javascript Javascript中的其他循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21525282/
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
For else loop in Javascript?
提问by
Is there a Javascript equivalent of the python 'for-else' loop, so something like this:
是否有与 Python 'for-else' 循环等效的 Javascript,例如:
searched = input("Input: ");
for i in range(5):
if i==searched:
print("Search key found: ",i)
break
else:
print("Search key not found")
Or do I just have to resort to a flag variable, so something like this:
或者我是否只需要求助于标志变量,这样的事情:
var search = function(num){
found = false;
for(var i in [0,1,2,3,4]){
if(i===num){
console.log("Match found: "+ i);
found = true;
}
}
if(!found){
console.log("No match found!");
}
};
采纳答案by Goran.it
Working example (you need to use the flag):
工作示例(您需要使用标志):
var search = function(num){
var found = false;
for(var i=0; i<5; i++){
if(i===num){
console.log("Match found: "+ i);
found = true;
break;
}
}
if(!found){
console.log("No match found!");
}
};
回答by Bergi
Yes, it is possible to do this without a flag variable. You can emulate for … else
statementsusing a labeland a block:
是的,可以在没有标志变量的情况下执行此操作。您可以使用标签和块来模拟for … else
语句:
function search(num) {
find: {
for (var i of [0,1,2,3,4]) {
if (i===num) {
console.log("Match found: "+ i);
break find;
}
} // else part after the loop:
console.log("No match found!");
}
// after loop and else
}
That said, I would recommend against doing this. It is a very unconvential way of writing this and will lead to poor understanding or confusion. An early return
is acceptable though, and can be used in a helper function if you need to continue with execution after the loop.
也就是说,我建议不要这样做。这是一种非常不合常规的书写方式,会导致理解不佳或混淆。return
不过,early是可以接受的,如果您需要在循环后继续执行,可以在辅助函数中使用它。
回答by Artyer
There is no built-in JavaScript equivalant.
没有内置的 JavaScript 等效项。
You can emulate this by using return
as a control flow. You can put your for
loop in an IIFE
and use the return to move beyond conditions afterwards. This does mean that var
s don't pollute the scope above with variables.
您可以通过return
用作控制流来模拟这一点。您可以将for
循环放在 an 中,IIFE
然后使用 return 超越条件。这确实意味着var
s 不会用变量污染上面的范围。
(function() { // Or `(() => {` in ES6 // Python equivalent:
for (/* for loop body*/) { // for <loop body>:
if (/* Condition */) { // if <Condition>:
// Met condition // <Met condition>
return; // Goes past the else // break
} //
}//else { // else:
// Never met the condition // <Never met condition>
//}
})();
This has the advantage of not using a flag. It is now inside another function, however, so you cannot declare variables to be used outside. You can still get and set variables in the scope above, so you just have to have the var
statements outside of the function.
这具有不使用标志的优点。但是,它现在位于另一个函数内部,因此您不能声明要在外部使用的变量。您仍然可以在上面的范围内获取和设置变量,因此您只需要var
在函数之外使用语句。
A working example for what you wanted to do:
您想要做的事情的工作示例:
(function(arr, value) {
for (var i = 0, length = arr.length; i < length; i++) {
if (arr[i] === value) {
console.log("Match found: " + arr[i]);
return;
}
}//else {
console.log("No match found!");
//}
})([0, 1, 2, 3, 4], +prompt("Input: "));
If you are doing this often, you can create a function to do most of it for you.
如果您经常这样做,您可以创建一个函数来为您完成大部分工作。
function search(arr, condition, forBody, elseBody) {
for (var i = 0, length = arr.length; i < length; i++) {
if (condition(arr[i], arr)) { // if
return forBody(arr[i], arr); // then
}
}
return elseBody(arr); // else
}
var value = +prompt("Input: ");
search([0, 1, 2, 3, 4],
i => /* if */ i === value,
i => /* then */ console.log("Match found: " + i),
() => /* else */ console.log("No match found!"));
回答by Emilio Rodriguez
In these cases you can do a straight check for the value
在这些情况下,您可以直接检查值
if (!(searched in range(5))) {
console.log("No match found!");
}else{
console.log(searched + " was found!");
}
回答by Cerbrus
You'll have to use the boolean. There's no for-else
in JavaScript.
你必须使用布尔值。for-else
JavaScript 中没有。
A nice and short way to search would be to use Array.prototype.indexOf()
:
一种不错且简短的搜索方法是使用Array.prototype.indexOf()
:
var search = function(num, arr){
var index = arr.indexOf(num);
if(index !== -1)
return "Match found: " + index;
}
return "No match found!";
};
Call it like this, for example:
像这样称呼它,例如:
console.log(search(4, [0,1,2,3,4]));
回答by iJade
You can either use a boolean
or you can simply return
. Some thing along this line should work...
您可以使用 aboolean
或者您可以简单地使用return
. 沿着这条线的一些事情应该工作......
var search = function(num){
found = false;
for(var i in [0,1,2,3,4]){
if(i===num){
console.log("Match found: "+ i);
return true;
}
}
return false;
};