Breaking out of an inner foreach loop

前端 未结 2 2042
终归单人心
终归单人心 2020-12-19 11:58

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

result.history.forEach(function(item) {
    loop2:
    item.forEach(function(inner         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 12:32

    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.

提交回复
热议问题