[英]Group Days for a week based on hours in each day
I'm really having issues understanding how to write the logic for the below requirement on javascript/typescript.我真的在理解如何为 javascript/typescript 的以下要求编写逻辑时遇到问题。 I need to use only javascript or typescript (no jquery/no other framework)
我只需要使用 javascript 或 typescript(没有 jquery/没有其他框架)
Here is the data in DB这是DB中的数据
Mon : 9:00AM - 7:00PM
Tue : 9:00AM - 10:00PM
Wed : 9:00AM - 7:00PM
Thu : 9:00AM - 7:00PM
Fri : 9:00AM - 7:00PM
Sat : 10:00AM - 4:00PM
Sun : 11:00AM - 5:00PM
I need to use the above data and start grouping in the Javascript/ Typescript我需要使用上述数据并在 Javascript/Typescript 中开始分组
The result after render in the javascript/typescript, it should look like在 javascript/typescript 中渲染后的结果应该是这样的
Mon,Wed- Fri : 9:00AM - 7:00PM
Tue : 9:00AM - 10:00PM
Sat : 10:00AM - 4:00PM
Sun : 11:00AM - 5:00PM
I'm sure someone can make it more elegant way, but this is how I solved the problem.我相信有人可以让它更优雅,但这就是我解决问题的方式。
let rows = [
"Mon : 9:00AM - 7:00PM",
"Tue : 9:00AM - 10:00PM",
"Wed : 9:00AM - 7:00PM",
"Thu : 9:00AM - 7:00PM",
"Fri : 9:00AM - 7:00PM",
"Sat : 10:00AM - 4:00PM",
"Sun : 11:00AM - 5:00PM"
]
/**
* We need to create an array, which will contain the days that will br grouped by AM and PM.
* Expected output:
* [
* {
* days: [
* {Mon: 0},
* {Wed: 2},
* {Thu: 3},
* {Fri: 4}
* ],
* am: 9:00AM
* pm: 7:00PM
* },
* {
* days: [
* {Tue: 1}
* ],
* am: 9:00AM,
* pm: 10:00PM
* }, ...
* ]
*/
let groups = [];
rows.forEach((row, index) => {
let parts = row.split(" : ");
let day = parts[0].trim();
let am = parts[1].split("-")[0].trim();
let pm = parts[1].split("-")[1].trim();
// Let's see if an item with the same AM and PM has been pushed into array.
let group = groups.find(x => x.am === am && x.pm === pm);
if (group) {
group.days.push({[day]: index});
} else {
let group = {
days: [{[day]: index}],
am: am,
pm: pm
}
groups.push(group);
}
})
/**
* Now, we can loop through groups array, and if there is more than a day in any array item,
* we will create a loop there. The tricky part is to create a string like Mon - Fri for consecutive days.
* To do that, I decided to check the index of day, prevDay, and nextDay. If they are consecutive numbers
* instead of adding the day to string add "-". If the string has already "-" at its end, just skip.
*/
let finale = [];
groups.forEach(group => {
if (group.days.length > 1) {
let days = "";
for (let i = 0; i < group.days.length; i++) {
let day = group.days[i];
let prevDay = group.days[i-1];
let nextDay = group.days[i+1];
let dayName = Object.keys(day)[0];
let dayIndex = Object.values(day)[0];
let prevDayIndex = prevDay ? Object.values(prevDay)[0] : null;
let nextDayIndex = nextDay ? Object.values(nextDay)[0] : null;
if (i === 0) {
days = dayName;
} else if (i === group.days.length - 1) {
days = `${days}, ${dayName}`;
} else {
if (nextDayIndex - dayIndex === 1 && dayIndex - prevDayIndex === 1) {
if (days.slice(-2) !== "- ") {
days = `${days} - `;
}
} else {
days = `${days}, ${dayName}`;
}
}
}
days = days.replace("- ,", "-")
finale.push(`${days} : ${group.am} - ${group.pm}`)
} else {
finale.push(`${Object.keys(group.days[0])[0]} : ${group.am} - ${group.pm}`)
}
})
console.log(finale)
finale = ["Mon, Wed - Fri : 9:00AM - 7:00PM", "Tue : 9:00AM - 10:00PM", "Sat : 10:00AM - 4:00PM", "Sun : 11:00AM - 5:00PM"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.