Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda