js 如何让forEach可以break?

While working with Java Script, all of us must have surely run into the case where we need to loop through an array and break the running loop if a certain condition is met. There are many methods how we can loop through an array but we are always looking for the most efficient way to do that.

Our favorite loop method: Array.prototype.forEach

forEach is such an useful method.
But…how can I break the forEach loop?
Well… you can’t break forEach.

1
2
3
4
5
6
var arr = [ "Kathmandu" , "Pokhara" , "Lumbini" , "Gorkha" ];
 
arr.forEach( function (value, index, _arr) {
console.log(index + ": " + value);
return false ;
});

The result should look like

1
2
3
4
0: Kathmandu
1: Pokhara
2: Lumbini
3: Gorkha

It’s still iterating through all items in the array. What should I do if I want to stop the loop based on the condition? We either set a variable that changes only if the condition is met, but it will still mean that the loop will iterate until the array is exhausted to the last item.

So, what do we do?? It’s simple.

Don’t use “forEach”. Use “some” or “every”.

Array.prototype.some

Array.prototype.some is pretty much the same as forEach but it break when the callback returns true.

Example:

1
2
3
4
5
6
var arr= [ "Kathmandu" , "Pokhara" , "Lumbini" , "Gorkha" ];
 
arr.some( function (value, index, _arr) {
console.log(index + ": " + value);
return value === "Pokhara" ;
});

The result should look like

1
2
0: Kathmandu
1: Pokhara

What happened here? Since the third iteration returned true, we successfully stopped the loop!


Array.prototype.every

Array.prototype.every is almost identical to some except it’s expecting false to break the loop.

Example:

1
2
3
4
5
6
var arr = [ "Kathmandu" , "Pokhara" , "Lumbini" , "Gorkha" ];
 
arr.every( function (value, index, _arr) {
console.log(index + ": " + value);
return value.indexOf( "Lumbini" ) < 0;
});

The result should look like

1
2
3
0: Kathmandu
1: Pokhara
2: Lumbini

What happened here? Since the third iteration returned false, we successfully stopped the loop!

Now you can break loops whenever you want! Enjoy.

Source:http://sajanmaharjan.com.np/2016/08/12/javascript-break-foreach/

转载于:https://www.cnblogs.com/backuper/p/10251398.html

JavaScript中,forEach方法无法使用break语句来提前终止循环。这是因为forEach方法的实现原理是通过for循环来遍历数组的,而for循环中无法使用break语句来跳出循环。只能使用return语句来跳过当前循环,相当于continue,而不能真正地跳出整个forEach循环。这是因为forEach方法的设计初衷是为了简化数组的遍历操作,提供一种简洁的语法来执行某个函数对数组中的每个元素进行操作,而不是为了提供类似于for循环的灵活性。所以,如果需要在遍历数组时能够使用break语句来提前终止循环,可以考虑使用普通的for循环或者for...of循环来替代forEach方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [JS forEach循环不能break或return](https://blog.csdn.net/CY2333333/article/details/117909358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [jsforEach以及forEach跳出循环](https://blog.csdn.net/m0_63476377/article/details/122458009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值