1 回答

TA貢獻1775條經(jīng)驗 獲得超11個贊
我們可以使用Object.entries、Array.filter和Array.reduce來計算所需日期范圍的總分。
我們只需要輸入正確的日期閾值:
let obj = {?
? ? "expHistory": {
? ? ? ? "2020-11-26": 84825,
? ? ? ? "2020-11-25": 87219,
? ? ? ? "2020-11-24": 44447,
? ? ? ? "2020-11-23": 14849,
? ? ? ? "2020-11-22": 57379,
? ? ? ? "2020-11-21": 32364,
? ? ? ? "2020-11-20": 42295
? ? }
}
? ? ? ? ? ??
function getTotal(thresholdDate, expHistory) {
? ? let result = Object.entries(expHistory)
? ? ? ? .filter(([date, points]) => date > thresholdDate)
? ? ? ? .reduce((total, [date, points]) => total + points, 0);
? ? return result;
}
?
const thresholdDate = "2020-11-20";
console.log(`Total (from ${thresholdDate}):`, getTotal(thresholdDate, obj.expHistory));
添加回答
舉報