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?
CodePudding user response:
No recursion needed. Loop over each day until you have enough entries.
const addProgramsBasedOnLength = (programs) => {
let offset = 0
while(programs.length < 10 && offset < 7) {
offset ;
const day = moment(selectedDate, FORMAT).add(offset, 'days').format(FORMAT);
const daysPrograms = programDateLookUp[nextDay];
if(daysPrograms) programs = programs.concat(daysPrograms);
}
return programs;
}
CodePudding user response:
Actually did something very similar to @Ouroborus comment, ended up 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. This allows to make sure I am 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