[英]Javascript: Determining the number of consecutive days (streak) in an array
我正在嘗試編寫一個 function,它獲取日期數組(可能未排序,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
我實現的 function 應該從開始日期向后創建一個日期范圍並計算數組中有多少日期在該范圍內。 每次循環,范圍的結束日期都會再往前一天。 只要范圍內的日期數量增加,循環就會繼續。
開始日期...開始日期 - 1 天 開始日期...開始日期 - 天
但是,由於某種原因,開始日期在 while 循環開始之前被結束日期覆蓋......
我將不勝感激任何幫助 - 或更好地解決問題的建議。 提前致謝!
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)
但是,由於某種原因,開始日期在 while 循環開始之前被結束日期覆蓋......
這就是您使用setDate
時發生的情況:它會改變日期。
減速器還不夠嗎?
[根據評論編輯]我有一個小圖書館來計算兩個可用日期之間的差異。 在 stackoverflow 片段中,這無法通過 javascript 面板加載,因此這是一個在 html 面板中使用腳本(類型模塊)的片段,利用了該庫。
<script type="module"> const diffCalc = (await import("//kooiinc.github.io/DateDifferenceCalculator/index.js")).default(); 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) => { const diff = i && diffCalc(d, self[i-1]); return diff && `${diff}`.startsWith(`1 day`)? [...acc, d]: acc; }, [] ); console.log(`Streak length: ${ streak.length}\nStreak values: ${ JSON.stringify(streak, null, 2)}`); </script>
所以在這里我們說一天的間隔 < 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 })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.