大佬教程收集整理的这篇文章主要介绍了如何在 javascript/typescript 中使日期比较更有效,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个异步数据,我正在使用这个函数从 Firebase Firestore 加载
graphDataSubscription;
//dataCounter;
pass@R_975_10586@lCalData;
@R_975_10586@lCalorIEs;
dates=[];
calorIEsData=[];
getGraphDateCalorIEs() {
if(this.graphDataSubscription){
this.graphDataSubscription.unsubscribe()
}
this.graphDataSubscription = this.mealservice.grabMealGraphDataCalorIEs(this.startDate,this.gatheredUID.uID).subscribe(items => {
this.dates = []
this.pass@R_975_10586@lCalData = []
//this.dataCounter = 0;
this.@R_975_10586@lCalorIEs=0;
this.calorIEsData=[];
items.forEach( item => {
//this.dataCounter++
var data: any = item.data()
this.pass@R_975_10586@lCalData.push(data.calorIEs)
this.calorIEsData.push(data);
this.@R_975_10586@lCalorIEs += data.calorIEs;
this.dates.push(moment(data.datE).locale(window.navigator.languagE).format("DD MMM"))
})
console.log("data acquired");
this.calendarData = this.getCalendarData();
this.calendarData = [...this.calendarData];
})
}
然后我试图将收集到的值插入到一个对象中,以在需要特定对象格式的图表中显示。这是一个热图图表,因此即使没有每一天的数据,它也必须包括前一年的全部天数。 like in this link
这是我做日期比较的函数,即使异步数据在一秒内收集也需要将近12秒。
calendarData: anY[] = [];
getCalendarData(): anY[] {
//console.log("begin")
// today
const Now = new Date();
const todaysDay = Now.getDate();
const thisDay = new Date(Now.getFullYear(),Now.getMonth(),todaysDay);
// Monday
const thisMonday = new Date(thisDay.getFullYear(),thisDay.getMonth(),todaysDay - thisDay.getDay() + 1);
const thisMondayDay = thisMonday.getDate();
const thisMondayYear = thisMonday.getFullYear();
const thisMondaymonth = thisMonday.getMonth();
// 52 weeks before monday
const calendarData = [];
const getDate = d => new Date(thisMondayYear,thisMondaymonth,d);
for (let week = -52; week <= 0; week++) {
const mondayDay = thisMondayDay + week * 7;
const monday = getDate(mondayDay);
// one week
const serIEs = [];
for (let dayOfWeek = 7; dayOfWeek > 0; dayOfWeek--) {
const date = getDate(mondayDay - 1 + dayOfWeek);
// skip future dates
if (date > Now) {
conTinue;
}
// value
var value = 0;
console.log("assigning to the object begins")
this.calorIEsData.forEach(data => {
if (data["date"] === date.tolocaleDateString('en-CA')) {
value = data["calorIEs"];
//console.log("condition granted");
return;
}
});
console.log("assigning to the object ends")
serIEs.push({
date,name: weekdayname.format(datE),value
});
}
calendarData.push({
name: monday.toString(),serIEs
});
}
return calendarData;
}
我知道这是太多的代码,但我不确定如何针对这个特定问题创建一个最小的示例。 那么有没有办法让这种比较更快呢?
这部分花费了很长时间,我的意思是它在两个嵌套的 for 循环中分别是一个 for each ,但我仍然不明白为什么比较这些日期需要这么多时间
var value = 0;
console.log("assigning to the object begins")
this.calorIEsData.forEach(data => {
if (data["date"] === date.tolocaleDateString('en-CA')) {
value = data["calorIEs"];
//console.log("condition granted");
return;
}
});
console.log("assigning to the object ends")
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的如何在 javascript/typescript 中使日期比较更有效全部内容,希望文章能够帮你解决如何在 javascript/typescript 中使日期比较更有效所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。