[英]how to search and compare the items in the array gotten from the output of a loop after the loop is complete (javascript)
请即时通讯,我的JavaScript代码有问题...。即时通讯正在Cordova的提醒应用程序上工作,即时通讯使用Katzer通知插件...现在我正在用javascript进行编码,即时通讯有一点挑战。 我正在尝试实现一项功能,当用户尝试添加已存在的提醒时,它将引发错误...并且如果该提醒不存在,则会将其添加到提醒列表中。我为此使用了JavaScript循环...这是我的代码
函数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();
}
});
}
});
}
我在上面尝试了这两种方法(for和while循环),但没有一个给我我的结果...相反,“ if()else”测试将在每个循环上单独运行...其缺点是,当在提醒列表的第一项setLecReminders()上进行测试; 函数运行,而不管后续测试是对还是错...我想要一个解决方案,其中循环首先完全运行,并且列表中的所有项目都输出到数组中,然后我可以对所有成员使用if()else
测试同时阵列。 请原谅我的长问题...在此先感谢
我建议像这样在循环中设置一个布尔值:
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++;
}
然后,您可以检查循环外的布尔值,并根据该结果显示警报:
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.