递归功能以检查数组的长度,如果其长度少于10,则从另一个数组添加到第一个数组,直到我达到10的长度
因此,我基本上要做的是查询一个星期的可用程序列表。我将其格式化为一个对象,以便在选择一天时,可以轻松地使用当天的对象,并创建programSelectedDay
。
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"
}
]
}
然后,我基于对象键抓住programSelectedDay
,所以
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"
}
]
现在我需要一个递归功能,我认为能够检查programSelectedDay
10,然后将第二天的程序添加到该programelectedDay
数组,依此类推,直到我至少达到10个结果,否则我已经度过了一周的剩余日子。
就目前而言,我有类似的东西,
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;
};
如果我只返回[... program,... nextdayPrograms]
而不是调用新数组的函数,它确实可以工作在第二天给我的结果。因此,我想知道如何使此递归最多将10个结果添加到原始的programelectedday
或直到一周的其余时间。
有什么知道做到这一点的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了一些与@ouroborus评论非常相似的事情。
相反,在当前选定的一周格式化日期的整个星期数组中循环;使用我已经选择的任何日期的索引。
下面的解决方案仅允许添加一周中的天数,即超过选定的日期,而不会在我选择的日期之前的几天内循环。
当我通过返回的数组映射时,将关键对象添加为UI的目的
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.
added the key object there for UI purposes when I map through the returned array