Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
for loop function called twice, for loop only occurs in second call

I have the following helper function I created, which is to serve to create an array of a date range. Assume that the holidays variable is an array of ISO date strings and is already populated elsewhere in my vue app.

export const parseAvailableDates = (
    startDate,
    endDate,
    holidays
) => {
    console.log({ startDate, endDate, holidays });
    let dates = [];
    for (
        let i = new Date(startDate);
        i <= new Date(endDate);
        i.setDate(i.getDate() + 1)
    ) {
        console.log("running");
        const thisDate = i.toLocaleString("en-US", localeString);
        if (!thisDate.includes("Sunday") && !thisDate.includes("Saturday")) {
            const formattedDate = changeLocaleDateToISOString(thisDate);
            if (!holidays.includes(formattedDate)) {
                dates.push(formattedDate);
            }
        }
    }
    console.log(dates);
    return dates;
};

Now, I need to call this function twice in my Vue app in the same function, once to initially populate the dates based on the range between startAssignments and lastDateToSubmitWork, and again to reset the availableDates array to the range between startAssignments and endDate.

// called here

    const startAssignments = `2022-02-09T00:00:00.000+04:00`
    const lastDateToSubmitWork = `2022-08-19T00:00:00.000+04:00`
    const endDate = `2022-08-31T00:00:00.000+04:00`

      let availableDates = [];
      availableDates = parseAvailableDates(
        startAssignments,
        lastDateToSubmitWork,
        holidays
      );
      console.log(availableDates);

// also called here

          availableDates = parseAvailableDates(
            startAssignments,
            endDate,
            holidays
          );

When these two calls happens, the initial console.log() in parseAvailableDates() shows the startDate, endDate, and holidays properly defined on both calls. However, on the first call, the for loop never runs, and the function returns [], and on the second call, the for loop does run, and the function returns a properly populated array of ISO date strings.

I don't understand why the first loop doesn't run properly, but the second does.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda