Javascript:确定数组中的连续天数(连续)

ehxuflar  于 2023-01-24  发布在  Java
关注(0)|答案(2)|浏览(226)

我正在尝试编写一个函数,该函数获取一个日期数组(可能是未排序的,date_array)作为输入,并确定数组中从某个日期(start_date)向后连续的天数,不包括开始日期。

  1. Example:
  2. start_date = 23.01.2023
  3. date_array = [
  4. 01.01.2023,
  5. 02.01.2023,
  6. 20.01.2023, <- day 3 of the streak
  7. 21.01.2023, <- day 2 of the streak
  8. 22.01.2023, <- day 1 of the streak
  9. 22.01.2023, <- day 1 of the streak
  10. 23.01.2023,
  11. 24.01.2023]
  12. Result:
  13. streak_lenght: 3 days

我实现的函数应该创建一个从开始日期向后的日期范围,并计算数组中有多少个日期在该范围内。每循环一次,该范围的结束日期就向前推进一天。只要该范围内的日期数量增加,循环就会继续。
开始日期...开始日期- 1天开始日期...开始日期-天
但是,由于某种原因,在while循环开始之前,start日期被end日期覆盖...
我将非常感谢任何帮助-或建议更好地解决这个问题。提前感谢!

  1. const dateArray = [
  2. new Date("2022-12-31"),
  3. new Date("2023-01-02"), // day 3 of streak
  4. new Date("2023-01-03"), // day 2 of streak
  5. new Date("2023-01-03"), // day 2 of streak
  6. new Date("2023-01-04"), // day 1 of streak
  7. new Date("2023-01-05")];
  8. const d_start = new Date("2023-01-05");
  9. function currentStreak(dateArray, d_start) {
  10. // dateArray: Array of dates on which event was executed
  11. // d_start: start date of the streak, not included
  12. // Create a range d_start ... d_end, d_end starts one day before d_start
  13. let d_end = d_start
  14. d_end.setDate(d_start.getDate() - 1)
  15. let countPrev = -1
  16. let count = 0
  17. let streakCount = 0
  18. // Count how many array elements are in the range and continue loop while this number increases
  19. while (count > countPrev) {
  20. countPrev = count
  21. // count number of array elements between start and end date
  22. count = dateArray.reduce((accumulator, currentValue) => {
  23. if((d_start > currentValue) && (currentValue > d_end))
  24. {
  25. accumulator += 1
  26. }
  27. return accumulator
  28. }, 0)
  29. // set new end date for next iteration
  30. d_end = d_end.setDate(d_end.getDate() - 1)
  31. streakCount = streakCount+1
  32. }
  33. return count;
  34. }
  35. currentStreak(dateArray, d_start)
oknrviil

oknrviil1#

但是,由于某种原因,在while循环开始之前,start日期被end日期覆盖...
这就是使用setDate时发生的情况:它会改变日期。
一个reducer还不够吗?类似于(假设开始日期是数组的最后一个日期):

  1. const dateArray = [
  2. new Date("2022-12-31"),
  3. new Date("2023-01-02"), // day 3 of streak
  4. new Date("2023-01-03"), // day 2 of streak
  5. new Date("2023-01-03"), // day 2 of streak
  6. new Date("2023-01-04"), // day 1 of streak
  7. new Date("2023-01-05") ];
  8. const streak = dateArray.reverse()
  9. .reduce( (acc, d, i, self) =>
  10. i && Math.abs(d.getDate() - self[i-1].getDate()) === 1
  11. ? [...acc, d] : acc, [] );
  12. console.log(
  13. streak.length,
  14. `\n`,
  15. streak );
展开查看全部
u0njafvf

u0njafvf2#

所以这里我们说一天的间隔〈26小时。
将所有运行推入累加器,然后弹出结果。

  1. const dateArray = [
  2. new Date("2022-12-31"),
  3. new Date("2023-01-02"), // day 3 of streak
  4. new Date("2023-01-03"), // day 2 of streak
  5. new Date("2023-01-03"), // day 2 of streak
  6. new Date("2023-01-04"), // day 1 of streak
  7. new Date("2023-01-05")]
  8. // 26Hrs is 9.36e+7;
  9. const streak = dateArray.reduce((p,day,i,arr) => {
  10. if ( arr[i+1]?.valueOf() == day.valueOf() ) return p;
  11. if ( arr[i+1]?.valueOf() - day.valueOf() < 9.36e+7 ) {
  12. p[p.length - 1].push(arr[i+1]);
  13. } else {
  14. p.push([])
  15. }
  16. return p;
  17. },[]).sort((a,b) => a.length - b.length).pop()
  18. console.log({ streak })
展开查看全部

相关问题