javascript 无法中断 for 循环:非语法中断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47017571/
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
Cannot break for-loop: Unsyntactic break
提问by Stophface
I want to breaka for-loopwhen a certain condition is met
我想break一个for-loop当满足特定条件
Object.keys(s).map(uk => {
Object.keys(s[uk]).map(ik => {
for (let i = 1; i < data.length; i++) {
if (...) {
s[uk][ik].map(elem => {
if (...) {
if (...) {
data.push(...);
break;
...
However, the breakstatement gives me a
然而,break声明给了我一个
Unsyntactic break
不合语法的中断
Why is that? Its only supposed to breakthe for-loop, or does JavaScriptthink that I want to break the map?
这是为什么?它只是想break了for-loop,还是JavaScript以为我要破解了map?
采纳答案by Neuron
Like you yourself suggested, you are still in the mappart. That is actually an arrow function expressionand you are no longer in in the loop. Think of it as some function you defined elsewhere, but it's a quicker way of calling that function.
就像您自己建议的那样,您仍然参与map其中。这实际上是一个箭头函数表达式,您不再处于循环中。将其视为您在别处定义的某个函数,但它是调用该函数的更快方法。
You are not using the map function as it is meant to be used in javascript. It isn't meant to be a convenient way to iterate over an array, but rather create a new array from some other array. You should change your usage of mapto some form of a loop
您没有使用 map 函数,因为它打算在 javascript 中使用。它并不是迭代数组的便捷方式,而是从其他数组创建一个新数组。您应该将使用更改map为某种形式的循环
回答by Максим Погребняк
You can't use breakwith methods like map, reduce, forEach, etc.
But you can .filteryour data before or just .findthe element you need
您不能使用break与类似方法map,reduce,forEach等,但你可以.filter你的数据之前或者只是.find你需要的元素

![javascript [Vue 警告]:渲染函数中的错误:“TypeError:无法读取 null 的属性‘first_name’”](/res/img/loading.gif)