My goal here is to query a list of available programs for a week-long period. I organize this data into an object where each day's programs are easily accessible through object look-up, creating programsOnSelectedDay
.
programDateLookUp {
"22/04/24": [
{
"programType": "CLINIC",
"start_date": "2022-04-24T16:00:00.000Z"
}
],
...
}
To retrieve the programsOnSelectedDay
, I use the Object keys like this:
const programsOnSelectedDay = programDateLookUp[selectedDay] || [];
where selectedDay = '22/04/25'
LOG programsOnSelectedDay [...]
Now, I need a recursive function to check the length of programsOnSelectedDay
, add the next day's programs to the array until there are at least 10 results or all days in the week have been accounted for.
The current implementation reaches maximum recursion depth, so I've come up with a potential solution:
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]
, it works but gives me results for one day only. I'm looking for guidance on how to modify this recursive function to either add up to 10 results to the original programsOnSelectedDay
or cover the entire week.
Any suggestions on how to achieve this?