Assuming you actually meant numeric indexes since you said "Array", here's a way to do it. The basic algorithm is that we create a bucket item that keeps track of the start day and end day for the current time. As we go through the array, if we find the same start/end time as what is in the bucket, we just update the end day. When we encounter a different set of start/end times, we push the current bucket onto the results array and start a new bucket with the new start/end times. When we're done, we have an array of buckets that tell us which buckets of start times we have and that can then be converted to a text form.
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;
}
Working demo: http://jsfiddle.net/jfriend00/rn9VC/
This returns the data in the order presented so Sunday would come first. If you want to put special case Sunday to go last, you can tweak the output to present it that way.
<div>s? How are you adding those values into the HTML show that code. If you are just inserting the raw value assomeDiv.innerHTML = [DAY_NAME] + map['n'];Then I would suggest splitting them into two divs, setting a class for the day and time and a width for each and having their display betable-cellor something.map['1']isn't really Array syntax? Arrays take numeric indexes.