开始默认全选,蓝色为选中的时间段,代码大致如下(点击时调用该方法):
let timeList = []
for (let i = 0; i < 24; i++) {
timeList[i] = {
name: i,
isClick: true
}
}
function countTimeArea(obj) {
const arr = []
let num = 0
let temp = 0
for (let i = 0; i < obj.length; i++) {
if (i === 0) {
if (obj[i].isClick) {
num++
temp = i
}
} else {
if (obj[i].isClick) {
num++
if (num === 1) {
temp = i
}
if (i === obj.length - 1) {
const str = temp > 9 ? temp : '0' + temp
const str2 = i > 9 ? i : '0' + i
arr.push(`${str}:00~${str2}:59`)
}
} else {
if (num > 0) {
const str = temp > 9 ? temp : '0' + temp
const str2 = i - 1 > 9 ? i - 1 : '0' + (i - 1)
arr.push(`${str}:00~${str2}:59`)
num = 0
}
}
}
}
return arr
}
console.log(countTimeArea(timeList))