显示开放时间算法javascript

时间:2014-03-14 22:58:27

标签: javascript algorithm

我正在寻找一种“好”的方式来显示某个位置的开放时间。

我给出了一个像这样的开放时间的数组:

map[1] = '10:00 - 18:00';
map[2] = '10:00 - 18:00';
map[3] = '09:00 - 18:00';
map[4] = '10:00 - 18:00';
map[5] = '10:00 - 18:00';
map[6] = '12:00 - 23:00';

想要得到给定数组的输出,如下所示:

Mo - Tu: 10:00 - 18:00
Wednesday: 09:00 - 18:00
Th - Fr: 10:00 - 18:00
Saturday: 12:00 - 23:00
Sunday: Closed

在这种情况下,星期日因为缺少map [0]而关闭。

2 个答案:

答案 0 :(得分:1)

假设您实际上意味着数字索引,因为您说过#34; Array",这是一种方法。基本算法是我们创建一个桶项,跟踪当前时间的开始日和结束日。当我们浏览数组时,如果我们找到与存储桶中相同的开始/结束时间,我们只更新结束日。当我们遇到一组不同的开始/结束时间时,我们将当前存储桶推送到结果数组并使用新的开始/结束时间启动新存储桶。当我们完成后,我们会有一系列的桶告诉我们我们拥有哪些开始时间桶,然后可以转换为文本格式。

var map = [];
map[1] = '10:00 - 18:00';
map[2] = '10:00 - 18:00';
map[3] = '09:00 - 18:00';
map[4] = '10:00 - 18:00';
map[5] = '10:00 - 18:00';
map[6] = '12:00 - 23:00';

function processHours() {

    var shortNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    var longNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    function makeBucket(day, hours) {
        var bucket = {};
        bucket.startDay = bucket.endDay = day;
        bucket.value = hours;
        bucket.hours = hours ? hours: "Closed";
        return bucket;
    }

    var buckets = [];
    var curBucket;
    for (var i = 0; i < 7; i++) {
        if (!curBucket) {
            curBucket = makeBucket(i, map[i]);
        } else {
            // if the current bucket has the same value as this day
            // then extend the range of the bucket
            if (map[i] === curBucket.value) {
                curBucket.endDay = i;            
            } else {
                // take current bucket and put it on the array
                buckets.push(curBucket);

                // start a new bucket
                curBucket = makeBucket(i, map[i]);            
            }
        }
    }
    // put last curBucket into the array
    buckets.push(curBucket);

    // now turn the set of buckets into array of strings
    var output = [];
    for (var i = 0; i < buckets.length; i++) {
        curBucket = buckets[i];
        // process single days different than multi-days
        if (curBucket.startDay === curBucket.endDay) {
            output.push(longNames[curBucket.startDay] + ": " + curBucket.hours);
        } else {
            output.push(shortNames[curBucket.startDay] + " - " + 
                        shortNames[curBucket.endDay] + ": " +
                        curBucket.hours);
        }
    }
    return output;
}

工作演示:http://jsfiddle.net/jfriend00/rn9VC/

这将按照显示的顺序返回数据,因此星期日将首先出现。如果你想把特殊情况星期日放到最后,你可以调整输出以这种方式呈现。

答案 1 :(得分:1)

这个更短。

使用索引7作为标记,并执行一个循环以获取输出。

DEMO

function foo( map ) {
    var names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var output = [];
    var lastidx = 0;
    var lastcont = map[0];
    for (var i = 0; i <= 7; ++i) {
        if( map[i] != lastcont || 7 == i) {
            lastcont = lastcont || "Closed";
            if( (i - 1) - lastidx > 0 ) {
                output.push( names[lastidx].slice(0,2)+" - "+names[i-1].slice(0,2)+": "+lastcont);
            }
            else {
                output.push( names[lastidx]+": "+lastcont); 
            }
            lastidx = i;
            lastcont = map[i];
        }
    };
    return output;
}

星期一的更新

function foo( map ) {
    var mapc = map.slice();
    mapc['7'] = mapc['0'];
    var names = ["", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
    var output = [];
    var lastidx = 1;
    var lastcont = mapc[lastidx];
    for (var i = lastidx; i <= 8; ++i) {
        if( mapc[i] != lastcont || 8 == i) {
            lastcont = lastcont || "Closed";
            if( (i - 1) - lastidx > 0 ) {
                output.push( names[lastidx].slice(0,2)+" - "+names[i-1].slice(0,2)+": "+lastcont);
            }
            else {
                output.push( names[lastidx]+": "+lastcont); 
            }
            lastidx = i;
            lastcont = mapc[i];
        }
    }
    return output;
}