Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove array of date using another array of date

I'm using MultipleDatePicker to select multiple date in year. I add a checkbox that when checked it will select all sunday in calendar.

I have a problem when unchecked it. It doesn't remove all selected sunday in calendar. I have compared by using getTime() as shown in the code below:

var selected = $scope.selectedDates;

for (var i = 0; i < $scope.selectedDates.length; i++) {
    var date1 = new Date(selected[i]).getTime();
    console.log('date1[' + i + '] = ' + date1 + ' ' + moment($scope.selectedDates[i], 'MM-DD-YYYY'));
    for (var j = 0; j < sundays.length; j++) {
        var date2 = new Date(sundays[j]).getTime();
        console.log('date2[' + j + '] = ' + date2 + ' ' + moment(sundays[j], 'MM-DD-YYYY'));
        if (date1 === date2) {
            selected.splice(i, 1);
            break;
        }
    }
}

Some are the same and some are not. What's wrong with the code?

Here is the plunker to show the problem.

like image 563
Willy Avatar asked Jun 11 '26 18:06

Willy


2 Answers

The problem is that you removed item from array and your index i has increased in a loop so one item got skipped. To fix this, decrease i after each removal:

    // ...
    if (date1 === date2) {
        selected.splice(i, 1);
        i--;
        break;
    }
like image 140
Milos Popovic Avatar answered Jun 13 '26 12:06

Milos Popovic


Little mistake, you forgot to decrement i, here is updated code.

for (var i = 0; i < $scope.selectedDates.length; i++) {
    var date1 = new Date(selected[i]).getTime();
    console.log('date1[' + i + '] = ' + date1 + ' ' + moment($scope.selectedDates[i], 'MM-DD-YYYY'));
    for (var j = 0; j < sundays.length; j++) {
        var date2 = new Date(sundays[j]).getTime();
        console.log('date2[' + j + '] = ' + date2 + ' ' + moment(sundays[j], 'MM-DD-YYYY'));
        if (date1 === date2) {
            selected.splice(i, 1);
            i--;
            break;
        }
    }
}
like image 45
Surjeet Bhadauriya Avatar answered Jun 13 '26 12:06

Surjeet Bhadauriya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!