admin管理员组文章数量:1026989
It is standard practice to continue
inside a loop if a certain condition is met/unmet. In a Javascript forEach
loop, this produces a syntax error:
const values = [1, 2, 3, 4, 5];
values.forEach((value) => {
if (value === 3) { continue; }
console.log(value);
})
SyntaxError[ ... ]: Illegal continue statement: no surrounding iteration statement
This happens whether I use function
or an arrow function. How can you continue
inside a forEach
loop?
Obviously, you could do an inverse case (if (value !== 3) { ... }
), but that is not what I'm looking for.
It is standard practice to continue
inside a loop if a certain condition is met/unmet. In a Javascript forEach
loop, this produces a syntax error:
const values = [1, 2, 3, 4, 5];
values.forEach((value) => {
if (value === 3) { continue; }
console.log(value);
})
SyntaxError[ ... ]: Illegal continue statement: no surrounding iteration statement
This happens whether I use function
or an arrow function. How can you continue
inside a forEach
loop?
Obviously, you could do an inverse case (if (value !== 3) { ... }
), but that is not what I'm looking for.
-
8
Use
return
rather thancontinue
. – Ouroborus Commented Jun 27, 2022 at 16:54 -
4
forEach()
isn't a loop, it's a function. If you want a loop, usefor (const value of values) { … }
– robertklep Commented Jun 27, 2022 at 16:54 -
@Ouroborus
return
rather thancontinue
does work correctly (+1) - it's behavior is not immediately obvious, however. It's easy to think that you are returning the parent function instead of the arrow function within theforEach
. – Chris Collett Commented Jun 27, 2022 at 18:13
3 Answers
Reset to default 3For practical purposes, return in a forEach() callback is equivalent to continue in a conventional for loop but it isn't the most idiomatic use of functional programming patterns
To solve that, you can filter before you loop:
const values = [1, 2, 3, 4, 5];
values.filter(v => v !== 3).forEach((value) => {
console.log(value);
})
in forEach method you can use return
, it will work like continue
in a loop
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Errors/Bad_continue
As @robertklep stated forEach() is not a loop, it is a function. You cannot use the continue
keyword inside a forEach loop because its functionality is meant to loop each item in the array.
To achieve the similar behavior you can use,
for(let item of values){
if (item === 3) {
continue;
}
console.log(item);
}
It is standard practice to continue
inside a loop if a certain condition is met/unmet. In a Javascript forEach
loop, this produces a syntax error:
const values = [1, 2, 3, 4, 5];
values.forEach((value) => {
if (value === 3) { continue; }
console.log(value);
})
SyntaxError[ ... ]: Illegal continue statement: no surrounding iteration statement
This happens whether I use function
or an arrow function. How can you continue
inside a forEach
loop?
Obviously, you could do an inverse case (if (value !== 3) { ... }
), but that is not what I'm looking for.
It is standard practice to continue
inside a loop if a certain condition is met/unmet. In a Javascript forEach
loop, this produces a syntax error:
const values = [1, 2, 3, 4, 5];
values.forEach((value) => {
if (value === 3) { continue; }
console.log(value);
})
SyntaxError[ ... ]: Illegal continue statement: no surrounding iteration statement
This happens whether I use function
or an arrow function. How can you continue
inside a forEach
loop?
Obviously, you could do an inverse case (if (value !== 3) { ... }
), but that is not what I'm looking for.
-
8
Use
return
rather thancontinue
. – Ouroborus Commented Jun 27, 2022 at 16:54 -
4
forEach()
isn't a loop, it's a function. If you want a loop, usefor (const value of values) { … }
– robertklep Commented Jun 27, 2022 at 16:54 -
@Ouroborus
return
rather thancontinue
does work correctly (+1) - it's behavior is not immediately obvious, however. It's easy to think that you are returning the parent function instead of the arrow function within theforEach
. – Chris Collett Commented Jun 27, 2022 at 18:13
3 Answers
Reset to default 3For practical purposes, return in a forEach() callback is equivalent to continue in a conventional for loop but it isn't the most idiomatic use of functional programming patterns
To solve that, you can filter before you loop:
const values = [1, 2, 3, 4, 5];
values.filter(v => v !== 3).forEach((value) => {
console.log(value);
})
in forEach method you can use return
, it will work like continue
in a loop
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Errors/Bad_continue
As @robertklep stated forEach() is not a loop, it is a function. You cannot use the continue
keyword inside a forEach loop because its functionality is meant to loop each item in the array.
To achieve the similar behavior you can use,
for(let item of values){
if (item === 3) {
continue;
}
console.log(item);
}
本文标签: javascriptContinue inside a forEach loopStack Overflow
版权声明:本文标题:javascript - Continue inside a forEach loop - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745554741a2155806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论