admin管理员组文章数量:1392101
II have a basic for loop to loop through a data feed:
for(var i = 0; i<items.length; i++) {
Then several variables are defined in this form:
var x = items.content[i]
Now I want to do something if one of the values of x
equals a value from outside the loop (y
):
if (x == y) {
//do something
}
All values of x
and y
will always be unique, so there can never be more than one match - but there may be none.
The problem es next - I want to do something else if there is no match, if no value of x
matches y
.
If I just do something like:
else {
//do something else
}
the else
condition is also satisfied by other values of x
. I have tried putting break;
after the if
condition, but unless the match is found on the first value of x
, both conditions are satisfied and both actions are triggered. How do I construct this so that the first action is triggered if there is a match between x
and y
, but the second action is triggered only if all values of x
don't match y
?
Thanks for any suggestions or advice.
II have a basic for loop to loop through a data feed:
for(var i = 0; i<items.length; i++) {
Then several variables are defined in this form:
var x = items.content[i]
Now I want to do something if one of the values of x
equals a value from outside the loop (y
):
if (x == y) {
//do something
}
All values of x
and y
will always be unique, so there can never be more than one match - but there may be none.
The problem es next - I want to do something else if there is no match, if no value of x
matches y
.
If I just do something like:
else {
//do something else
}
the else
condition is also satisfied by other values of x
. I have tried putting break;
after the if
condition, but unless the match is found on the first value of x
, both conditions are satisfied and both actions are triggered. How do I construct this so that the first action is triggered if there is a match between x
and y
, but the second action is triggered only if all values of x
don't match y
?
Thanks for any suggestions or advice.
Share Improve this question asked Jul 4, 2014 at 15:01 sideroxylonsideroxylon 4,3961 gold badge24 silver badges42 bronze badges 1- 2 You can declared one boolean at the top and mark it to TRUE if value matches, later you can refer that variable throughout your process surely :) – K D Commented Jul 4, 2014 at 15:04
1 Answer
Reset to default 4Create a boolean that will hold the result of finding 'y', then execute the actions outside of the loop depending on that boolean:
var match = false;
for(var i = 0; i<items.length; i++) {
var x = items.content[i]
if(x == y) {
match = true;
// a match is found, there is no need to continue,
break;
}
}
if(match) {
//do something
} else {
//do something else
}
本文标签: javascriptConditional statement inside for loopStack Overflow
版权声明:本文标题:javascript - Conditional statement inside for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768758a2624205.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论