"else if" statement within for loop within larger if/else if/else statement
Is it possible to do something like this in JavaScript?
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
for (i in exampleObject) {开发者_JAVA技巧
else if (name == exampleObject[i].name) {
exampleFunction(exampleObject[i].arg);
}
}
else {
exampleFunction('default')
}
I tried it, but got an "unexpected keyword else on line 8" (the "else if" within the for loop). Is there another way to do this?
edit: updated this to use exampleObject[i] in the loop. My bad!
No. I think the best way to accomplish this is to move the for
loop into an else
block and do the following
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
var isFound = false;
for (i in exampleObject) {
if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
isFound = true;
}
}
if (!isFound) {
exampleFunction('default')
}
}
Note: It looks like there are other errors in this code. The for
loop declares the i
iteration variable but never actually uses it. Did you mean for the if
check in the for
loop to use i
instead of name
?
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
var isFound = false;
for (i in exampleObject) {
if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
isFound = true;
break;
}
}
if (!isFound) {
exampleFunction('default')
}
}
Here is the correct solution. It short circuts the if statements in the loop just like else if would short circuit. This is the same solution as #1 but it correctly short circuits.
The following code looks wrong to me , have the for loop inside if block
for (i in exampleObject) {
else if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
}
that is not possible. I would try an come up with a better example to show you how to do what you want, but honestly I am not sure what you want to do. The for loop is confusing me. Can you provide some more information?
In a word, no. You are terminating the if-statement block with the last brace before the for statement.
Well for one, shouldn't this:
for (i in exampleObject) {
else if (name == exampleObject.name) {
exampleFunction(exampleObject.arg);
}
}
be this:
for (i in exampleObject) {
else if (name == i.name) {
exampleFunction(i.arg);
}
}
Though i don't know much (if anything) about js, this is just a guess at something that isn't even the problem you're talking about.
Would you be adverse to doing it like this:
bit = 0;
if (name == 'foo') {
exampleFunction('some_arg');
}
else if (name == 'bar') {
exampleFunction('another_arg');
}
else {
bit = 1;
}
bit2 = 0;
while(bit == 1){
for (i in exampleObject) {
if (name == i.name) {
exampleFunction(i.arg);
bit = 0
bit2 = 1;
}
}
}
if(bit2 = 0){
exampleFunction('default');
}
?
Something like this may help?
found = false
if (name == 'foo') {
found = true
exampleFunction('some_arg');
}
else if (name == 'bar') {
found = true
exampleFunction('another_arg');
}
else {
for (i in exampleObject) {
if (name == i.name) {
exampleFunction(i.arg);
found = true
break;
}
}
}
if !found:
exampleFunction('default')
精彩评论