2017-02-16 89 views
-1

問題是,一旦我在JSON對象中找到「date」,我的for循環不停地滴答滴答。我認爲這導致我每次進入條件的「其他」部分。在jQuery/JavaScript中如何在滿足特定條件時退出for循環

我對JavaScript/jQuery相當陌生。從控制檯

report-generation.js:443 Entering Else Loop for 02/13/2017. The date from the JSON file is 02/6/2017 
report-generation.js:434 Entering If Loop for 02/13/2017. The date from the JSON file is 02/13/2017 
report-generation.js:443 Entering Else Loop for 02/13/2017. The date from the JSON file is 02/20/2017 
report-generation.js:488 02/6/2017 date from JSON. Selected Date is 02/13/2017 
report-generation.js:488 02/13/2017 date from JSON. Selected Date is 02/13/2017 
report-generation.js:488 02/20/2017 date from JSON. Selected Date is 02/13/2017 

輸出我的JavaScript代碼

// Get Information Technology Division Manager Schedule 
for (var i = 0; i < informationtechnologydivisionmanager.length; i++) 
{ 
    var emailAddressParts = informationtechnologydivisionmanager[i].name.split(' '); 
    var emailAddress = emailAddressParts[0] + "." + emailAddressParts[1] + "@example.com"; 
    var createLink = "mailto:" + emailAddress + "?subject=Schedule for " + dateFromInput + "&body=This is a template. Please remove these lines and add your own"; 

    if (informationtechnologydivisionmanager[i].date == theSelectedDate) 
    { 
     console.log("Entering If Loop for " + theSelectedDate + ". The date from the JSON file is " + informationtechnologydivisionmanager[i].date); 
     $('.informationtechnologydivisionmanagerName').html("<a href='" + createLink + "'>" + informationtechnologydivisionmanager[i].name + "</a><br>"); 
     $('.informationtechnologydivisionmanagerTitle').html(informationtechnologydivisionmanager[i].title); 
     $('.informationtechnologydivisionmanagerMondaySchedule').html(informationtechnologydivisionmanager[i].mondayAM + "<br>" + informationtechnologydivisionmanager[i].mondayPM); 
     $('.informationtechnologydivisionmanagerTuesdaySchedule').html(informationtechnologydivisionmanager[i].tuesdayAM + "<br>" + informationtechnologydivisionmanager[i].tuesdayPM); 
     $('.informationtechnologydivisionmanagerWednesdaySchedule').html(informationtechnologydivisionmanager[i].wednesdayAM + "<br>" + informationtechnologydivisionmanager[i].wednesdayPM); 
     $('.informationtechnologydivisionmanagerThursdaySchedule').html(informationtechnologydivisionmanager[i].thursdayAM + "<br>" + informationtechnologydivisionmanager[i].thursdayPM); 
     $('.informationtechnologydivisionmanagerFridaySchedule').html(informationtechnologydivisionmanager[i].fridayAM + "<br>" + informationtechnologydivisionmanager[i].fridayPM); 
    } else { 
     console.log("Entering Else Loop for " + theSelectedDate + ". The date from the JSON file is " + informationtechnologydivisionmanager[i].date); 
     $('.informationtechnologydivisionmanagerName').html("<a href='" + createLink + "'>" + informationtechnologydivisionmanager[i].name + "</a><br>"); 
     $('.informationtechnologydivisionmanagerTitle').html(informationtechnologydivisionmanager[i].title); 
     $('.informationtechnologydivisionmanagerMondaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set"); 
     $('.informationtechnologydivisionmanagerTuesdaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set"); 
     $('.informationtechnologydivisionmanagerWednesdaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set"); 
     $('.informationtechnologydivisionmanagerThursdaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set"); 
     $('.informationtechnologydivisionmanagerFridaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set"); 
    } 
} 

MYHTML代碼

<table id="informationtechnology" cellspacing="0" cellpadding="0"> 
    <thead> 
     <tr> 
      <th colspan="6" class="caption">Information Technology</th> 
     </tr> 
     <tr> 
      <th><span>Name</span></th> 
      <th><span>Monday</span></th> 
      <th><span>Tuesday</span></th> 
      <th><span>Wednesday</span></th> 
      <th><span>Thursday</span></th> 
      <th><span>Friday</span></th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td class="lalign"><span class="informationtechnologydivisionmanagerName"></span><span class="informationtechnologydivisionmanagerTitle" style="color: navy;text-align: center;"></span></td> 
      <td><span class="informationtechnologydivisionmanagerMondaySchedule"></span></td> 
      <td><span class="informationtechnologydivisionmanagerTuesdaySchedule"></span></td> 
      <td><span class="informationtechnologydivisionmanagerWednesdaySchedule"></span></td> 
      <td><span class="informationtechnologydivisionmanagerThursdaySchedule"></span></td> 
      <td><span class="informationtechnologydivisionmanagerFridaySchedule"></span></td>    
     </tr> 
    </tbody> 
</table> 
+1

[破](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break)是,如果以通常的方式 –

+0

(條件)休息; –

+0

willl break讓我離開if循環或for循環? –

回答

2

用來走出一個循環的兩個主要關鍵字breakcontinue
break完全停止循環,並在循環後跳到該行。
continue只是停止當前的迭代,並跳轉到循環的開始,繼續下一次迭代。

所以,你需要的是break

相關問題