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