time()
function time() {
var timeData = ['10:00-11:00', '11:00-12:00', '12:00-13:00', '14:00-15:00'
, '16:00-17:00', '17:00-18:00', '18:00-19:00', '19:00-20:00', '21:00-22:00',
'22:00-23:00', '23:00-24:00']
getTime(timeData)
console.log( getTime(timeData))
}
function getTime(timeData) {
let newtimeDate = []
for (let i = 0; i < timeData.length; i++) {
if (i == timeData.length - 1) {
newtimeDate.push(timeData[i])
} else {
if (timeData[i + 1].slice(0, 5) === timeData[i].slice(6)) {
newtimeDate.push(`${timeData[i].slice(0, 5) + timeData[i + 1].slice(5)}`)
if (timeData.length % 2 == 1 && i == timeData.length - 3) {
newtimeDate.push(timeData[timeData.length - 1])
i += 2
} else {
i += 1
}
} else {
newtimeDate.push(timeData[i])
}
}
}
if (newtimeDate.length >= timeData.length) {
return newtimeDate
}
return getTime(newtimeDate)
}
var arr = [{ time: "10:00-12:00", week: 0 }, { time: "10:00-12:00", week: 1 },
{ time: "10:00-12:00", week: 2 }, { time: "10:00-12:00", week: 3 },
{ time: "10:00-13:00", week: 4 }, { time: "10:00-13:00", week: 6 },
{ time: "10:00-13:00", week: 7 },
{ time: "11:00-14:00", week: 8 },];
const groupBy = (array, f) => {
let groups = {};
array.forEach(function (o) {
var group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function (group) {
return groups[group];
});
};
const arrayGroupBy = (list, groupId) => {
let sorted = groupBy(list, function (item) {
return [item[groupId]];
});
return sorted;
};
let groupTime = arrayGroupBy(arr, 'time');
groupTime.forEach((item) => {
item.forEach((e) => {
})
})
combinedWeek()
function combinedWeek() {
const statusEnum = {
1: "周一",
2: "周二",
3: "周三",
4: "周四",
5: "周五",
6: "周六",
7: "周日",
};
let workdayList = [
{
time: "09:00-12:00",
workday: "1,2,3",
},
{
time: "08:00-12:00",
workday: "4,5",
},
{
time: "18:00-20:00",
workday: "1,4,5",
}
];
workdayList.forEach((item1) => {
var days = item1.workday.split(",");
for (let index = 0; index < days.length; index++) {
const dayItem = days[index];
if (dayItem === "0") {
days[index] = "7";
}
}
days = days.sort();
const start = [];
const end = [];
var reload = true;
for (let index = 0; index < days.length; index++) {
const element = days[index];
const element1 = days[index + 1];
if (reload) {
start.push(element);
}
if (element1 - element === 1) {
reload = false;
continue;
} else {
reload = true;
end.push(element);
}
}
var resultMsg = [];
for (let index1 = 0; index1 < start.length; index1++) {
const startItem = start[index1];
const endItem = end[index1];
if (endItem - startItem === 0) {
resultMsg.push(statusEnum[startItem]);
} else {
resultMsg.push(
statusEnum[startItem] + "至" + statusEnum[endItem])
}
}
console.log(resultMsg);
item1.resultMsg =
resultMsg.join("、") +
" " +
item1.time
});
}
console