结合重复"嵌套"阵列的Javascript(使用每周打开和关闭的时间) [英] Combining Duplicate "Nested" Arrays Javascript (Using for weekly open and closed hours)
问题描述
这一个是我的烘焙面大的时候!
我有一个数组,看起来像这样里面的一些阵列
VAR lidHours = {192611:[
[07:00,21:30],
[07:00,21:30],
[07:00,21:30],
[09:00,21:30],
[09:00,00:00]
[08:00,08:00],
[,]
]
}
和的工作日数组
VAR平日=新的Array(星期一,星期二,星期三,星期四,星期五,星期六,太阳);
因此,每个阵列重新presents一周中的一天。我到输出这样的页面:周一:07:00 - 21:00等等...
但你可以看到,第3天有相同的时间,我的目标是把它显示是这样的:
周一至周三:7:00 - 9:00
周四:9:00 - 9:30
周五:9:00 - 12:00
星期六:8:00 - 8:00
孙:关闭
我可以格式化小时,知道他们什么时候关闭,军事转换为标准,但我不能弄清楚如何重复小时(周一至周三)结合起来,我怎么能preserve星期一,知道了最后重复一天周三??
任何帮助将是如此之大的AP preciated。谢谢!
VAR combined_hours = [];
VAR openHours = lidHours [192611];
VAR prevHours;
对于(VAR天= 0;日间及LT; openHours.length;天++){
VAR curHours = openHours [日]
如果(curHours [0]!= prevHours [0] || curHours [1]!= prevHours [1]){
combined_hours.push({朝九特派:平日[日]
endday指定:,
开放:curHours [0]
收盘:curHours [1]});
prevHours = curHours;
}其他{
combined_hours [combined_hours.length-1] .endDay =' - '+平日[日]
}
}
现在可以显示combined_hours。
This one is baking my noodle big time!
I have some arrays inside an array that looks like this
var lidHours = {"192611":[
["07:00","21:30"],
["07:00","21:30"],
["07:00","21:30"],
["09:00","21:30"],
["09:00","00:00"],
["08:00","08:00"],
["",""]
]
}
And an array of the week days
var weekDays = new Array("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun");
So each array represents a day of the week. I'm outputting onto the page like this: Monday: 7:00 - 21:00 and so on...
But you can see that the first 3 days have the exact same hours, my goal is to have it display like this:
Mon-Wed: 7:00 - 9:00
Thu: 9:00 - 9:30
Fri: 9:00 - 12:00
Sat: 8:00 - 8:00
Sun: Closed
I can format the hours and know when they are closed and convert from military to standard, but I cant figure out how to combine the duplicate hours(Mon-Wed) and how can I preserve Mon and know that the last duplicate day was Wed??
Any help would be so greatly appreciated. Thanks!
var combined_hours = [];
var openHours = lidHours["192611"];
var prevHours;
for (var day = 0; day < openHours.length; day++) {
var curHours = openHours[day];
if (curHours[0] != prevHours[0] || curHours[1] != prevHours[1]) {
combined_hours.push({ startDay: weekDays[day],
endDay: "",
open: curHours[0],
close: curHours[1] });
prevHours = curHours;
} else {
combined_hours[combined_hours.length-1].endDay = '-' + weekdays[day];
}
}
Now you can display combined_hours.
这篇关于结合重复&QUOT;嵌套&QUOT;阵列的Javascript(使用每周打开和关闭的时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!