在each()和forEach()中使用break和continue [英] Using break and continue in each() and forEach()

查看:183
本文介绍了在each()和forEach()中使用break和continue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们不能使用break和continue关键字,我不确定我是否理解函数式循环/映射的值。

I am not sure I understand the value of the functional style looping/mapping if we can't use the break and continue keywords.

我可以这样做:

collections.users.models.forEach(function(item, index) {
    //can't use break or continue...?
});

或者我可以这样做:

for (var i = 0; i < collections.users.models.length; i++) {
     if (user.username === collections.users.models[i].username) {
         app.currentUser = collections.users.models[i];
         break;
     }
}

如果可以的话,功能调用有什么优势? t使用break或continue关键字?

what's the advantage of the functional call if I can't use the break or continue keywords?

推荐答案

使用 Array.prototype.some Array.prototype.every 继续/中断函数for循环是一件好事

Using Array.prototype.some and Array.prototype.every to continue/break out of functional for loops is a good thing to do

[1,2,3,null,5].every(function(v){
    if(!v) return false;  // this will break out of the loop
    // do your regular loopage lulz
});

这篇关于在each()和forEach()中使用break和continue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆