简体   繁体   中英

in jQuery/JavaScript how do I exit a for loop when specific conditions are met

The problem is that once I find the "date" in the JSON Object, my for loop keeps ticking. I assume this is causing me to enter the "Else" part of the condition everytime.

I am fairly new to JavaScript/jQuery.

Output from Console

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

My JavaScript Code

// 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 Code

<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>

The two main keywords used to get out of a loop are break and continue .
break stops the loop completely and jumps to the line after the loop.
continue just stops the current iteration, and jumps to the beginning of the loop, continuing with the next iteration.

So what you need is break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM