In my current code, I have a loop that verifies if a date is a holiday and then adds it to an array. The issue I'm facing is that whenever I assign a new element to the array, all previous data in the list gets updated to the latest element. Here's a glimpse of the problem:
if (
this._dateHelperService.isBusinessDay(temporalDate) &&
!this._dateHelperService.isHoliday(temporalDate)
) {
mNumberOfDays += 1;
console.log(temporalDate.toDateString());
console.log('entre')
let tmpDate: Date[] = [...result.datesToPaint, temporalDate];
result.datesToPaint = tmpDate;
console.table(result.datesToPaint)
// console.log(mNumberOfDays);
}
// re creating the date using a helper service
if (mNumberOfDays !== numberOfDays) {
temporalDate =
this._dateHelperService.addDays(temporalDate,1);
}
// helper service:
addDays(date: Date, days: number): Date {
date.setDate(date.getDate() + days);
return date;
}
This issue could be due to the pointer I'm using for the global list result.datesToPaint at the beginning of the method. I attempted to use the spread operator and temporarily assign the date to another variable, but it didn't resolve the problem. Any assistance on this matter would be greatly appreciated!