组合重复的“嵌套”数组Javascript(用于每周开放和关闭时间)

时间:2014-03-13 22:38:52

标签: javascript arrays duplicates combinations

这个是我的面条很大的时间!

我在数组中有一些看起来像这样的数组

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 weekDays = new Array("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun");

因此每个数组代表一周中的某一天。我这样输出到页面上:星期一:7:00-21:00等等......

但你可以看到前3天完全相同,我的目标是让它显示如下:

Mon-Wed: 7:00 - 9:00
Thu: 9:00 - 9:30
Fri: 9:00 - 12:00
Sat: 8:00 - 8:00
Sun: Closed

我可以格式化时间并知道它们何时关闭并从军事转换为标准,但我无法弄清楚如何结合重复小时数(周一至周三)以及如何保存周一并知道最后一天重复是星期三?

任何帮助都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

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];
    }
}

现在你可以显示combined_hours。