我想增加價值的天和星期幾內每天,即上午,下午,晚上和夜間,因爲我迭代了一個集合,我得到然後評估時間是在哪個時隙。 我需要能夠遍歷前端的最終結果,但想要一個更好的解決方案,即我目前代表的一個更好的解決方案,但總計數組。有人建議我使用地圖,但不確定如何使用地圖來實現此功能。二維數組或地圖存儲價值與平日和插槽天
var totals = [
// Tot M T W T F S S
[0, 0, 0, 0, 0, 0, 0, 0], // Totals
[0, 0, 0, 0, 0, 0, 0, 0], // Morning
[0, 0, 0, 0, 0, 0, 0, 0], // Afternoon
[0, 0, 0, 0, 0, 0, 0, 0], // Evening
[0, 0, 0, 0, 0, 0, 0, 0] // Night
];
var collectionOfData = entiredataset;
collectionOfData.forEach(function (item) {
var localDate = getLocalDate(item);//gets users local date and determines timeslot id - ie Morning,afternoon, evening, or night
var dayOfWeek = localDate.day();
var timeslotId = item.timeslotId;
totals[timeslotId][dayOfWeek]++; // Increase sessions per slot and day
totals[0][dayOfWeek]++; // Increase total sessions per day
totals[timeslotId][0]++; // Increase total sessions per slot
totals[0][0]++; // Increase total sessions
}
任何建議將不勝感激。