简体   繁体   English

根据每天的小时数分组一周的天数

[英]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.

相关问题 按常见营业时间分组工作日排列 - Group week days array by common open hours 包含按星期几的商店营业时间的组对象 - Group object that contains store hours by day of week js为一周中的每一天选择小时 - js select hours for each day of the week 使用时刻js创建一个星期几和一天中几小时的数组? - Using moment js to create an array with days of the week and hours of the day? 根据星期几获取星期几数组 - Get week days array based on week day number 使用Javascript根据星期几和一天中的时间显示HTML元素 - Using Javascript to Show HTML elements based on Days of the week, and Time of the day 如何按天、1 小时、6 小时、8 小时、周、月和年对日期数组和总和值进行分组? - How to group array of dates and sum value by day, 1hour, 6hours, 8hours, week, month, and year? 如何在javascript中按小时/6小时/天/周/月/年对日期对象进行分组 - How to group date object by hour/6 hours /day/week/month/year in javascript 如何在 7 天的一周内对时间戳进行分组? - how to group timestamp in a week of 7 days? 如何在使用 JS 的天气预报应用程序中为 5 天中的每一天显示正确的星期几? - How do I display the correct day of the week for each of the 5 days in a weather forecast app using JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM