Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
Need Help properly assigning a Date to a Date[] without modifying existing elements in Typescript Array

I have the following loop that checks if the date is holiday and thereon, fills up the Date[].

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;
        }

The issue is that every time I assign a new element all the list data gets modified to the latest element. Here is a snapshot of the issue:

Sample output

I'm aware this may be related to the pointer I have on the global list I created at the start of the method for result.datesToPaint. I tried using the spread operator and assigning the date to another date temporally. Still not working. Thanks for the help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thanks to @Tobias S. The solution was to adjust my addDays method to the following:

addDays(date: Date, days: number): Date {
    return new Date(date.setDate(date.getDate() + days));
  }

:) Be careful when populating date arrays!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!