我有一个功能,我在移动设备上安排提醒。我正在构建一个重复项的数组,并递增提交给数组的每个对象的两个属性,对象中的其他所有内容都保持不变。当我开始循环的第一次迭代时,它按预期将对象推送到数组中,但是第二次迭代在我调试之前进入对象或推送函数之前更新数组中的'reminderId'值。 在下一次迭代中,它会递增,最后得到一个如下所示的数组: 应该是这样的: 请注意,日期正确递增,但是'reminderId'增加的方式存在问题,而且我已经敲了几个小时。 我的代码构建如下。这可能是非常简单的事情,非常感谢任何帮助。[
0{id: 2, title: "Athens", text: "Test", at: Tue Dec 13 2016 16:00:28 GMT-0600 (CST)}
1{id: 3, title: "Athens", text: "Test", at: Wed Dec 14 2016 16:00:28 GMT-0600 (CST)}
2{id: 3, title: "Athens", text: "Test", at: Thu Dec 15 2016 16:00:28 GMT-0600 (CST)}
]
[
0{id: 1, title: "Athens", text: "Test", at: Tue Dec 13 2016 16:00:28 GMT-0600 (CST)}
1{id: 2, title: "Athens", text: "Test", at: Wed Dec 14 2016 16:00:28 GMT-0600 (CST)}
2{id: 3, title: "Athens", text: "Test", at: Thu Dec 15 2016 16:00:28 GMT-0600 (CST)}
]
if(isIOS){
var notificationItem = [];
var reminderId = -1;
for(i=0; i < 3; i++){
var iOSNotification = {};
if ($scope.scheduledContainer.length == 0 && notificationItem.length == 0) {
reminderId = 1;
} else if ($scope.scheduledContainer.length > 0) {
var convertDate = new Date(time_to_notify);
convertDate.setDate(convertDate.getDate()+1);
time_to_notify = convertDate;
var max = $scope.scheduledContainer.reduce(function(prev, current) {
return (prev.y > current.y) ? prev : current
}); //$scope.scheduledContainer contains array items if they alreay exist
reminderId = ++max.id;
} else if (notificationItem.length > 0) {
var convertDate = new Date(time_to_notify);
convertDate.setDate(convertDate.getDate()+1);
time_to_notify = convertDate;
var max = notificationItem.reduce(function(prev, current) {
return (prev.y > current.y) ? prev : current
}); //notificationItem is a temp array that contains first time notification repeats
reminderId = ++max.id;
}
iOSNotification = {
id: reminderId,
title: mxAppConfig.RegionId,
text: task.msg,
at: time_to_notify
}
notificationItem.push(iOSNotification);
}
}
答案 0 :(得分:0)
更改预增量
reminderId = ++max.id;
后增量
reminderId = max.id++;
答案 1 :(得分:0)
我发现了这个问题。我在将它推入数组之前添加了对象的引用。这是一个对象的实例,它将自动更新&#39; id&#39;数组中的属性。在我从if / else语句中收集值时删除它并构建对象构建了一个每次都要推送的新对象。