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

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

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

Example:
start_date = 23.01.2023

date_array = [
01.01.2023,
02.01.2023,
20.01.2023, <- day 3 of the streak
21.01.2023, <- day 2 of the streak
22.01.2023, <- day 1 of the streak
22.01.2023, <- day 1 of the streak
23.01.2023,
24.01.2023]

Result:
streak_lenght: 3 days

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

const dateArray = [
  new Date("2022-12-31"), 
  new Date("2023-01-02"), // day 3 of streak
  new Date("2023-01-03"), // day 2 of streak 
  new Date("2023-01-03"), // day 2 of streak
  new Date("2023-01-04"), // day 1 of streak
  new Date("2023-01-05")];

const d_start = new Date("2023-01-05");

function currentStreak(dateArray, d_start) {
    // dateArray: Array of dates on which event was executed
    // d_start: start date of the streak, not included

    // Create a range d_start ... d_end, d_end starts one day before d_start

    let d_end = d_start
    d_end.setDate(d_start.getDate() - 1)
    let countPrev = -1
    let count = 0
    let streakCount = 0

    // Count how many array elements are in the range and continue loop while this number increases    
    while (count > countPrev) {
        countPrev = count
        // count number of array elements between start and end date
        count = dateArray.reduce((accumulator, currentValue) => {
          if((d_start > currentValue) && (currentValue > d_end))
          {
            accumulator += 1
          }
         return accumulator
        }, 0)
        // set new end date for next iteration
        d_end = d_end.setDate(d_end.getDate() - 1)
        streakCount = streakCount+1
    }
    return count;
  }

currentStreak(dateArray, d_start)
oknrviil

oknrviil1#

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

const dateArray = [
  new Date("2022-12-31"), 
  new Date("2023-01-02"), // day 3 of streak
  new Date("2023-01-03"), // day 2 of streak 
  new Date("2023-01-03"), // day 2 of streak
  new Date("2023-01-04"), // day 1 of streak
  new Date("2023-01-05") ];

const streak = dateArray.reverse()
  .reduce( (acc, d, i, self) => 
    i && Math.abs(d.getDate() - self[i-1].getDate()) === 1
      ? [...acc, d] : acc, [] );
      
console.log(
  streak.length,
  `\n`,
  streak );
u0njafvf

u0njafvf2#

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

const dateArray = [
  new Date("2022-12-31"), 
  new Date("2023-01-02"), // day 3 of streak
  new Date("2023-01-03"), // day 2 of streak 
  new Date("2023-01-03"), // day 2 of streak
  new Date("2023-01-04"), // day 1 of streak
  new Date("2023-01-05")]
  
// 26Hrs is 9.36e+7;

const streak = dateArray.reduce((p,day,i,arr) => {
    if ( arr[i+1]?.valueOf() == day.valueOf() ) return p;
    if ( arr[i+1]?.valueOf() - day.valueOf() <  9.36e+7 ) { 
      p[p.length - 1].push(arr[i+1]); 
    } else { 
      p.push([]) 
    }
    return p;
},[]).sort((a,b) => a.length - b.length).pop()

console.log({ streak })

相关问题