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

176
Vistas
Recursive function to check length of an array and if its less than 10 in length, add from another array to the first array until I hit 10 in length

So what I am basically trying to do is I query for a list of available programs for a week period. I format that into an object for when selecting a single day, I can easily use object look up for that day, and create programsOnSelectedDay.

programDateLookUp {
  "22/04/24": [
    {
      "programType": "CLINIC",
      "start_date": "2022-04-24T16:00:00.000Z"
    }
  ],
  "22/04/25": [
    {
      "programType": "PRACTICE",
      "start_date": "2022-04-25T16:00:00.000Z"
    },
    {
      "programType": "PICKUP",
      "start_date": "2022-04-25T16:00:00.000Z"
    }
  ],
  "22/04/27": [
    {
      "programType": "PRACTICE",
      "start_date": "2022-04-27T16:00:00.000Z"
    }
  ],
  "22/04/28": [
    {
      "programType": "CLINIC",
      "start_date": "2022-04-28T16:00:00.000Z"
    }
  ]
}

I then grab the programsOnSelectedDay based on the Object keys like so

const programsOnSelectedDay = programDateLookUp[selectedDay] || [];
where selectedDay = '22/04/25'
 LOG  programsOnSelectedDay [
  {
    "programType": "PRACTICE",
    "start_date": "2022-04-25T16:00:00.000Z"
  },
  {
    "programType": "PICKUP",
    "start_date": "2022-04-25T16:00:00.000Z"
  }
]

Now I need a recursive function I think to be able to check the length of programsOnSelectedDay, see that it is less then 10, and add the next day programs to that programsOnSelectedDay array, and so on until I hit at least 10 results or I have gone through the remaining days in the week.

As it stands, I have something like this which will just hit maximum depth exceeded

  const addProgramsBasedOnLength = (programs) => {
    if (programs.length === 0) return programs;

    if (programs.length < 10) {
      const nextDay = moment(selectedDate, FORMAT).add(1, 'days').format(FORMAT);
      const nextDayPrograms = programDateLookUp[nextDay] || [];
      return addProgramsBasedOnLength([...programs, ...nextDayPrograms]);
    }
    return programs;
  };

If I only return [...programs,...nextDayPrograms] instead of the function calling that new array, it does work in giving me the next day results only. So I'm wondering how to make this recursive to give me up to 10 results added to the original programsOnSelectedDay OR until the rest of the week as been gone through.

Any know of a way to accomplish this?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I tried something very similar to @Ouroborus comment.
Instead looping through the week array that had formatted dates for the current selected week; using the index of whatever date I have already selected.
Below solution allows to only adding days of the week that is past the selectedDate and not looping over days that are before my selectedDate.

const addProgramsBasedOnLength = (programs, programDateLookUp, selectedDate, week) => {
  if (programs.length >= 10) return programs;

  const indexOfSelectedDate = week.findIndex(day => day.formatted === selectedDate);

  const restOfWeekPrograms = week.slice(indexOfSelectedDate + 1).reduce((acc, { formatted }) => [
    ...acc,
    ...programDateLookUp[formatted] || [],
  ], []);

  return [...programs, { key: 'noMoreResultsForDay' }, ...restOfWeekPrograms];
};

added the key object there for UI purposes when I map through the returned array

about 4 years ago · Santiago Trujillo Denunciar
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