码上敲享录 > vue.js常见问题详解 > js的forEach循环中return后还执行方法后面的代码

js的forEach循环中return后还执行方法后面的代码

上一章章节目录下一章 2019-11-12已有3503人阅读 评论(0)

js的forEach循环中return后还执行方法后面的代码


解决方法:

1.错误代码,return false后还还执行$(this.$refs.cuteModal).modal('hide');

         this.cutes.forEach(function(item){

             if (!item.cited_qid || !item.cited_type||!item.cited_var) {

                 showWarnTips('选项不能为空');

                 return false;

             }

         });

          $(this.$refs.cuteModal).modal('hide');

 

2.改进后正确代码,加flag标识,因为在js循环中的return只是跳出循环体

        var flag=true;

         this.cutes.forEach(function(item){

             if (!item.cited_qid || !item.cited_type||!item.cited_var) {

                 flag=false;

                 showWarnTips('选项不能为空');

                 return false;

             }

         });

         if(flag){

             $(this.$refs.cuteModal).modal('hide');

         }


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交