简体   繁体   中英

how to search and compare the items in the array gotten from the output of a loop after the loop is complete (javascript)

please im having a bit of a problem with my javascript code.... im working on a reminder app in Cordova and im using Katzer notification plugin... now i am coding in javascript and im having a little challenge. im trying to achieve a feature whereby , when a user tries to add a reminder that already exists, it will throw an error... and if the reminder dosen't exist, it will add it to the list of reminders.... im using as javascript loop for this... heres my code

function checkifReminderExists(){

 cordova.plugins.notification.local.getAll(function (notifications) {

     var allRemInfo = "";
     var newAllRemInfo = "";

// using while loop  
     var count = 0;



while (count < notifications.length) {

 cordova.plugins.notification.local.get(count, function (notification) {


             allRemInfo = allRemInfo + notification.text ;


if(allRemInfo.indexOf(""+checkedBoxes+"") == true)
        {
               alert("sorry you cant add a reminder that already exist...");

           } else {

alert("there is no similarity so im going ahead to create the reminder now...");
            setLecReminders();                       
                }


          });
         count++  
         continue;   

     }  

         });   

}    


     /* the above did not work so i tried using for loop to achieve this

function checkifReminderExists(){

 cordova.plugins.notification.local.getAll(function (notifications) {

     var allRemInfo = "";
     var newAllRemInfo = "";
    var count;


 for(count = 0; count < notifications.length; count++)                      
                { 


cordova.plugins.notification.local.get(count, function (notification) {

 allRemInfo = allRemInfo + notification.text + ", " ;

 newAllRemInfo = new Array(""+allRemInfo+"");

 if(newAllRemInfo.indexOf(""+checkedBoxes+"") == true)
 {
   alert("sorry you cant add a reminder that already exist...");

 } else 
     {
     alert("there is no similarity so im going ahead to create the reminder now...");
     setLecReminders();                      
     }


         });


                }


         });   

}

I tried both methods(for and while loop) above and none of them gave me my result... instead the "if()else" test will run separately on each of the loop...the disadvantage of this, is that when a test runs on the first item in the list of reminders, the setLecReminders(); function runs irrespective of if the subsequent test are true or false... i want a solution whereby the loop runs completely first and all items on the list are outputted into an array and then i can use a if()else test on all members of the array simultaneously. Please pardon my long question... thanks in advance

I would suggest setting a boolean inside of your loop like this:

    var reminderExists = false;
    while (count < notifications.length)
    {
        cordova.plugins.notification.local.get(count, function (notification) {
            allRemInfo = allRemInfo + notification.text;
            if (allRemInfo.indexOf(""+checkedBoxes+"") == true)
            {
                reminderExists = true;
            }
            count++;
    }

Then you can check the boolean outside of the loop and show your alerts based on that result:

    if (reminderExists) {
        alert("sorry you cant add a reminder that already exist...");
    } else {
        alert("there is no similarity so im going ahead to create the reminder now...");
        setLecReminders();
    }

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