我有一個物件,其中包含為一周中的每一天指定的辦公時間/作業時間。我的目標是能夠簡化物件的文本輸出,而無需重復與前一天的作業時間相同的不必要的作業日作業時間。
當前輸出如下:
Monday: 08:00 AM - 21:00 PM
Tuesday: 08:00 AM - 21:00 PM
Wednesday: 08:00 AM - 21:00 PM
Thursday: 08:00 AM - 21:00 PM
Friday: 08:00 AM - 21:00 PM
Saturday: 08:00 AM - 18:00 PM
我正在執行以下操作以顯示每天 ( it should skip any days with the start and end time set to 00:00 AM - such as Sunday)的作業時間的文本輸出:
// Office hours object
const json_office_hours = '{"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"},"Thursday":{"start":"08:00 AM","end":"21:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}}';
const office_hours = JSON.parse(json_office_hours);
// Iteration
let office_hours_text = "";
let oh_prev_day = "Monday";
for (let day in office_hours) {
if (office_hours.hasOwnProperty(day)) {
let oh_start = office_hours[day].start;
let oh_end = office_hours[day].end;
if (oh_start !== '00:00 AM' && oh_end !== '00:00 AM') {
office_hours_text = day ": " oh_start " - " oh_end '\n';
}
oh_prev_day = day;
}
}
console.log(office_hours_text);
我試圖實作的簡化輸出如下:
Monday - Friday: 08:00 AM - 21:00 PM
Saturday: 08:00 AM - 18:00 PM
出于某種原因,我無法簡化輸出。
您的幫助將不勝感激 - 謝謝。
uj5u.com熱心網友回復:
你可以做的是在你回圈的時候對這些日子進行分組,現在你發現一個組的末尾將它添加到文本中
由于該解決方案在休息后的第二天回圈時列印,因此您需要在回圈后使用代碼來列印最后一組
// Office hours object
const json_office_hours = '{"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"},"Thursday":{"start":"08:00 AM","end":"21:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}}';
const office_hours = JSON.parse(json_office_hours);
// Iteration
let office_hours_text = "";
let oh_group = {
start_day: "Monday",
end_day: "Monday",
start: office_hours.Monday.start,
end: office_hours.Monday.end
}
for(let day in office_hours) {
if(office_hours.hasOwnProperty(day)) {
let oh_start = office_hours[day].start;
let oh_end = office_hours[day].end;
if (oh_start === oh_group.start && oh_end === oh_group.end) {
// add to the group
oh_group.end_day = day;
} else {
// add to the text
if(oh_group.start !== '00:00 AM' && oh_group.end !== '00:00 AM') {
if (oh_group.start_day === oh_group.end_day) {
office_hours_text = oh_group.start_day;
} else {
office_hours_text = oh_group.start_day " - " oh_group.end_day;
}
office_hours_text = ": " oh_group.start " - " oh_group.end '\n';
}
// and reset the group
oh_group = {
start_day: day,
end_day: day,
start: office_hours[day].start,
end: office_hours[day].end
};
}
}
}
// since the loop prints when it finds the end of a group,
// the last group needs to be printed outside of the loop
if(oh_group.start !== '00:00 AM' && oh_group.end !== '00:00 AM') {
if (oh_group.start_day === oh_group.end_day) {
office_hours_text = oh_group.start_day;
} else {
office_hours_text = oh_group.start_day " - " oh_group.end_day;
}
office_hours_text = ": " oh_group.start " - " oh_group.end '\n';
}
console.log(office_hours_text);
uj5u.com熱心網友回復:
我會從簡單的開始,按作業時間對日子進行分組。從那里,您可以進一步簡化出現的日期。按下按鈕以查看幾種可能的組合。它也適用于例如星期三關閉時。
function parseHours(officeHours) {
// A mapping of office hours to the days to which they are appliccable
const officeHourDays = {};
Object.entries(officeHours).forEach(([day, { start, end }]) => {
if(start === '00:00 AM' && end === '00:00 AM') { return; }
const ohString = `${start} - ${end}`;
// Default to an empty list if this is the first time we see these office hours
if(!officeHourDays[ohString]) { officeHourDays[ohString] = []; }
officeHourDays[ohString].push(day);
});
// We now have an object { hourString: [days] }, we need to transform
// it to the output string
let officeHoursText = Object
.entries(officeHourDays)
.map(([hours, days]) => {
const dayString = days.join(', ');
return `${dayString}: ${hours}`;
})
.join('\n');
// Loop over all possible combinations of subsequent days
// (and what they should be replaced with), from longest
// to shortest (best to worst match)
const daysOfTheWeek = Object.keys(officeHours);
// length - 1 because we don't care about sequences of 2 days or less
for(let start = 0; start < daysOfTheWeek.length - 1; start ) {
for(let end = daysOfTheWeek.length - 1; end > start 1; end--) {
// Friday, Saturday, Sunday
const originalString = daysOfTheWeek.slice(start, end 1).join(', ');
// Friday - Sunday
const shortString = `${daysOfTheWeek[start]} - ${daysOfTheWeek[end]}`;
officeHoursText = officeHoursText.replace(originalString, shortString);
}
}
console.log(officeHoursText);
}
<button onclick='parseHours({"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"},"Thursday":{"start":"08:00 AM","end":"21:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}})'>Monday - Friday: 08:00 AM - 21:00 AM, Saturday 08:00 AM - 18:00 PM</button>
<br/>
<button onclick='parseHours({"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"}})'>Monday - Wednesday: 08:00 AM - 21:00 AM</button>
<br/>
<button onclick='parseHours({"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"18:00 PM"},"Thursday":{"start":"08:00 AM","end":"21:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}})'>Monday, Tuesday, Thursday, Friday: 08:00 AM - 21:00 AM, Wednesday, Saturday 08:00 AM - 18:00 PM</button>
<br/>
<button onclick='parseHours({"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"},"Thursday":{"start":"08:00 AM","end":"18:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}})'>Monday - Wednesday, Friday: 08:00 AM - 21:00 AM, Thursday, Saturday 08:00 AM - 18:00 PM</button>
uj5u.com熱心網友回復:
您可以通過和之間的連接字串對其進行分組oh_start,oh_end并使用陣列來保存這些作業時間的天數。從那里開始只回圈遍歷分組元素并格式化文本。如果只有一天有 thouse 作業時間,就不要顯示最后一個元素
// Office hours object
const json_office_hours = '{"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"},"Thursday":{"start":"08:00 AM","end":"21:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}}';
const office_hours = JSON.parse(json_office_hours);
// Iteration
let office_hours_text = [];
let group_by_working = {};
for (let day in office_hours) {
if (office_hours.hasOwnProperty(day)) {
let oh_start = office_hours[day].start;
let oh_end = office_hours[day].end;
if (oh_start !== '00:00 AM' && oh_end !== '00:00 AM') {
if (!group_by_working[oh_start oh_end])
group_by_working[oh_start oh_end] = {start: oh_start, end: oh_end, days: []};
group_by_working[oh_start oh_end].days.push(day);
}
}
}
//Format output
for (key in group_by_working) {
let group = group_by_working[key];
if (group.days.length > 1) {
office_hours_text.push(group.days[0] " - " group.days.slice(-1) " : " group.start " - " group.end);
} else {
office_hours_text.push(group.days[0] " : " group.start " - " group.end);
}
}
console.log(office_hours_text.join("\n"));
uj5u.com熱心網友回復:
const json_office_hours = '{"Monday":{"start":"08:00 AM","end":"21:00 PM"},"Tuesday":{"start":"08:00 AM","end":"21:00 PM"},"Wednesday":{"start":"08:00 AM","end":"21:00 PM"},"Thursday":{"start":"08:00 AM","end":"21:00 PM"},"Friday":{"start":"08:00 AM","end":"21:00 PM"},"Saturday":{"start":"08:00 AM","end":"18:00 PM"},"Sunday":{"start":"00:00 AM","end":"00:00 AM"}}';
let office_hours = JSON.parse(json_office_hours);
let office_days = [];
for (value in office_hours){
office_days.push(value " " office_hours[value].start " " office_hours[value].end)
console.log(office_hours[value].start)
}
console.log(office_days)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/378780.html
標籤:javascript
下一篇:打字稿中泛型型別的嚴格打字問題
