简体   繁体   English

循环完成后,如何搜索和比较从循环输出中获得的数组中的项目(javascript)

[英]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. 请即时通讯,我的JavaScript代码有问题...。即时通讯正在Cordova的提醒应用程序上工作,即时通讯使用Katzer通知插件...现在我正在用javascript进行编码,即时通讯有一点挑战。 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 我正在尝试实现一项功能,当用户尝试添加已存在的提醒时,它将引发错误...并且如果该提醒不存在,则会将其添加到提醒列表中。我为此使用了JavaScript循环...这是我的代码

function checkifReminderExists(){ 函数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(); 我在上面尝试了这两种方法(for和while循环),但没有一个给我我的结果...相反,“ if()else”测试将在每个循环上单独运行...其缺点是,当在提醒列表的第一项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. 函数运行,而不管后续测试是对还是错...我想要一个解决方案,其中循环首先完全运行,并且列表中的所有项目都输出到数组中,然后我可以对所有成员使用if()else测试同时阵列。 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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