[英]JavaScript splice() method not removing time items in array that are half past the hour?
I have 3 arrays containing time periods that are strings... like so: 我有3个包含时间段的数组,这些时间段是字符串...就像这样:
$scope.shiftMorning = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30'];
$scope.shiftAfternoon = ['12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30'];
$scope.shiftEvening = ['18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00', '22:30', '23:00', '23:30'];
Now I have a series of functions to determine the current time to the next 30 minutes, and then I compare the current time against the values in the array. 现在,我具有一系列函数来确定到下一个30分钟的当前时间,然后将当前时间与数组中的值进行比较。 If the current time is after the array value we remove the array value... like so:
如果当前时间在数组值之后,我们将删除数组值...,如下所示:
function dateCompare(time1, time2) {
//console.log("time 1 = " + time1 + " time 2 = " +time2);
var t1 = new Date();
var parts = time1.split(":");
t1.setHours(parts[0], parts[1], "00", 0);
var t2 = new Date();
parts = time2.split(":");
t2.setHours(parts[0], parts[1], "00", 0);
// returns 1 if greater, -1 if less and 0 if the same
if (t1.getTime() > t2.getTime()) {
return 1;
} else if (t1.getTime() < t2.getTime()) {
return -1;
} else {
return 0;
}
}
function getNearestHalfHourTimeString() {
var now = new Date();
var hour = now.getHours();
var minutes = now.getMinutes();
if (minutes < 15) {
minutes = "30";
} else if (minutes < 45) {
minutes = "00";
hour = hour + 1;
} else {
minutes = "00";
++hour;
}
return(hour + ":" + minutes);
}
var currentTime = getNearestHalfHourTimeString();
function removeUnavailableTimes(timeArray) {
for (var i = 0; i < timeArray.length; i++) {
console.log(timeArray[i], currentTime, dateCompare(timeArray[i], currentTime));
if (dateCompare(timeArray[i], currentTime) === -1) {
// remove the offending item from the array
timeArray.splice(i, 1);
}
}
//console.log(timeArray);
return timeArray;
}
removeUnavailableTimes($scope.shiftMorning);
removeUnavailableTimes($scope.shiftAfternoon);
removeUnavailableTimes($scope.shiftEvening);
Now all seems to be fine however items that are in the past (our current time is greater) that have a half hour value ('09:30') don't seem to be removed from the array (using .splice()) however I notice that my comparison is correct. 现在一切似乎都很好,但是过去(当前时间更长)具有半小时值('09:30')的项目似乎没有从数组中删除(使用.splice())但是我注意到我的比较是正确的。 Can any one see what I am doing wrong?
有人可以看到我在做什么吗?
You are removing array elements with .splice()
while you iterate over it. 迭代时,您正在使用
.splice()
删除数组元素。 When you remove, for example, timeArray[0]
, the rest of the array is shifted, so that when you examine timeArray[1]
you have actually skipped an element. 例如,当删除
timeArray[0]
,数组的其余部分将移动,因此当您检查timeArray[1]
,实际上已经跳过了一个元素。
You need to use another system, like: 您需要使用其他系统,例如:
The code is almost there. 代码几乎就在那里。 When
splice()
run, timeArray
is changed because splice()
pop the variable and index is changed at that run time. 当
splice()
运行时, timeArray
会更改,因为splice()
弹出该变量,并且索引会在该运行时更改。 Please check my code. 请检查我的代码。
function removeUnavailableTimes(timeArray) {
var i = 0;
while( i < timeArray.length ){
console.log(timeArray[i], currentTime, dateCompare(timeArray[i], currentTime));
if (dateCompare(timeArray[i], currentTime) === -1) {
// remove the offending item from the array
timeArray.splice(i, 1);
}else{
i++;
}
}
//console.log(timeArray);
return timeArray;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.