最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Breaking out of an inner foreach loop - Stack Overflow

programmeradmin4浏览0评论

I am trying to break out of an inner foreach loop using JavaScript/jQuery.

result.history.forEach(function(item) {
    loop2:
    item.forEach(function(innerItem) {
        console.log(innerItem);
        break loop2;
    });
}); 

This is resulting in the error 'Unidentified label loop2'. it appears to be right before the loop which was what other questions were saying was the issue.

What am I doing wrong and how do I fix it?

Edit: Correct, the foreach loop cant break in this way but a regular for loop can. This is working:

                        result.history.forEach(function(item) {
                            loop2:
                            for (var i = 0; i < item.length; i++) {
                                var innerItem = item[i];
                                console.log(innerItem);
                                break loop2;
                            }
                        });

I am trying to break out of an inner foreach loop using JavaScript/jQuery.

result.history.forEach(function(item) {
    loop2:
    item.forEach(function(innerItem) {
        console.log(innerItem);
        break loop2;
    });
}); 

This is resulting in the error 'Unidentified label loop2'. it appears to be right before the loop which was what other questions were saying was the issue.

What am I doing wrong and how do I fix it?

Edit: Correct, the foreach loop cant break in this way but a regular for loop can. This is working:

                        result.history.forEach(function(item) {
                            loop2:
                            for (var i = 0; i < item.length; i++) {
                                var innerItem = item[i];
                                console.log(innerItem);
                                break loop2;
                            }
                        });
Share Improve this question edited Dec 22, 2015 at 23:08 David Tunnell asked Dec 22, 2015 at 22:54 David TunnellDavid Tunnell 7,54222 gold badges74 silver badges128 bronze badges 6
  • 2 You can't use break to do that. – Pointy Commented Dec 22, 2015 at 22:56
  • stackoverflow./a/183197/5442132 – myselfmiqdad Commented Dec 22, 2015 at 22:57
  • @miqdadamirali Yes, that is the link I am referring to. – David Tunnell Commented Dec 22, 2015 at 22:58
  • @Pointy I believe you can but you really shouldn't. – Mike Cluck Commented Dec 22, 2015 at 22:59
  • 1 @Pointy Guess I should have verified before I opened my big mouth haha. I learned something new today. – Mike Cluck Commented Dec 22, 2015 at 23:03
 |  Show 1 more ment

2 Answers 2

Reset to default 9

If you need to be able to break an iteration, use .every() instead of .forEach():

someArray.every(function(element) {
  if (timeToStop(element)) // or whatever
    return false;
  // do stuff
  // ...
  return true; // keep iterating
});

You could flip the true/false logic and use .some() instead; same basic idea.

Can't do it. According to MDN:

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behaviour, the .forEach() method is the wrong tool, use a plain loop instead. If you are testing the array elements for a predicate and need a boolean return value, you can use every() or some() instead.

发布评论

评论列表(0)

  1. 暂无评论