forEach如何终止循环


try {

try {
  this.list.forEach(item => {
     .....
    throw new Error('end')
  })  
} catch(err) {
   console.log('err',err)
  }
// 实例
 confirm() {
      try {
        this.questions.surveyModuleContentDTOS.forEach((item, index) => {
          if (item.moduleType === '02') {
            console.log('子组件', this.$refs['module' + index][0].checkSelf())
            const result = this.$refs['module' + index][0].checkSelf()
            if (!result.flag) {
              alertDialog({
                title: '温馨提示',
                message: `请完成 ${result.questionItem.surveyQuestionDetailDTO.subject}`
              })
              this.score = 0
              throw new Error('end') // 终止循环
            }
            this.score += result.score
          }
        })
        console.log('执行到这里,说明校验通过了发送请求', this.score)
        alertDialog({
          title: '提示',
          message: `您的测评得分为 ${this.score}分`,
          buttonCallback: () => {
            console.log('传给后端的数据', this.questions)
          }
        })
      } catch (err) {
        console.log('err', err)
      }
    }

  

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM