我为办公室时间表提供了CSV格式,并在同一时间表的日子中使用了破折号范围。

例如,今天是Thursday


  星期一3:00 am-9:00 pm,星期二-星期四5:30 am-9:30 pm,星期五7:15 am-10:00 pm,
  周六8:00 am-3:00 pm,星期日休息


我需要删除周日关闭的时间表,例如,如果今天是Thursday,则首先显示第一个Thursday

我的预期输出是这样的:


  星期二-星期四5:30 am-9:30 pm,星期五7:15 am-10:00 pm,星期六8:00 am-3:00 pm,星期一3:00 am-9:00 pm


如果今天为Thursday(当前日期),则为一些示例和预期结果:

'Mon 4:00am - 9:00pm, Tue 6:00am - 9:30pm, Wed - Fri 5:30am - 9:30pm, Sat 8:00am - 3:00pm, Sun Closed'预期结果:'Tue 6:00am - 9:30pm, Wed - Fri 5:30am - 9:30pm, Sat 8:00am - 3:00pm, Mon 4:00am - 9:00pm'

'Wed 5:30am - 9:30pm, Thu 6:30am - 4:00pm, Fri - Tue 8:00am - 3:00pm, Sun Closed'预期结果:'Thu 6:30am - 4:00pm, Fri - Tue 8:00am - 3:00pm, 'Wed 5:30am - 9:30pm'

'Mon - Tue 5:30am - 9:30pm, Wed 6:30am - 4:00pm, Thu - Sat 8:00am - 3:00pm, Sun Closed'预期结果:'Thu - Sat 8:00am - 3:00pm, Mon - Tue 5:30am - 9:30pm, Wed 6:30am - 4:00pm'

我当前的代码部分正常工作:

function sortDays(days) {
    var dayOfWeek = new Date().getDay();
    var list = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    var sortedList = list.slice(dayOfWeek).concat(list.slice(0, dayOfWeek));
    return days.sort(function(a,b) { return sortedList.indexOf(a) > sortedList.indexOf(b); });
}

function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax= arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}

function getDaysRange(first, last) {
    // var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var week = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

    // Find first day
    var firstIndex = week.indexOf(first);
    // Shift array so that first day is index 0
    week = week.concat(week.splice(0, firstIndex));
    // Find last day
    var lastIndex = week.indexOf(last);

    // Cut from first day to last day and convert to CSV string
    // return week.slice(0, lastIndex + 1).toString();
    return week.slice(0, lastIndex + 1);
}



function reorderSchedule(schedule) {

    var detailedSchedule = [];
    var daysInWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    var scheduleExploded = schedule.split(',');
    var schedulehtms = '';

    console.log('===============================>>>>>>>>>>>>>>>>>>>');
    for (var i = 0, len = scheduleExploded.length; i < len; i++) {
        schedulehItem = scheduleExploded[i].trim();
        console.log(schedulehItem + ' = LOOP NUMBER ' + i + ' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');

        var count = (schedulehItem.match(/-/g) || []).length;
        console.log(count + " piece(s) of - found inside " + schedulehItem);

        if (count >= 2) {
            console.log(schedulehItem + ' is a range! ===============');

            // break down the days range
            var search = schedulehItem.search(/\d/);

            var scheduleOfRange = schedulehItem.substring(search, schedulehItem.length);
            console.log(scheduleOfRange);
            var daysRange = schedulehItem.substring(0, search - 1);
            console.log(daysRange);

            var arr = daysRange.split('-');
            var first = arr[0].trim();
            var second = arr[1].trim();

            var daysRange = getDaysRange(first, second);
            daysRange = removeA(daysRange, 'Sun');
            console.log(daysRange);
            console.log('>>>>>>>');

            for (x = 0; x < daysRange.length; x++) {
                detailedSchedule.push(daysRange[x] + ' ' + scheduleOfRange);
            }

        } else {
            // insert the schedule in the final array
            detailedSchedule.push(schedulehItem);
        }

    }
    console.log('===============================>>>>>>>>>>>>>>>>>>>');
    console.log('=============');
    console.log(detailedSchedule);
    console.log('=============');

    var origDaysOnlyOrder = [];
    var daysWithScheduleOrder = [];
    var sortedDays = sortDays(daysInWeek);

    for (i = 0; i < detailedSchedule.length; i++) {

        var myNewStr = detailedSchedule[i].substr(0, 3);
        // origDaysOnlyOrder.push(myNewStr);
        daysWithScheduleOrder[myNewStr] = detailedSchedule[i];
    }

    console.log('vvvvvvvvvvvvvvvvvvvvvvvvvvvv');
    console.log(daysWithScheduleOrder);
    console.log(sortedDays);

    var finalArray = [];
    for (i = 0; i < sortedDays.length; i++) {
        // don't insert undefined values
        if (typeof(daysWithScheduleOrder[sortedDays[i]]) != 'undefined') {
            // finalArray.push(daysWithScheduleOrder[sortedDays[i]]);
            finalArray[sortedDays[i]] = daysWithScheduleOrder[sortedDays[i]].substr(3).trim();
        }
    }

    // remove
    console.log(finalArray);
    console.log('######################>>>');

    // now in correct order then bring back the dash in the original range, check for same schedules then create the range
    // find first

    var scheduleObj = {};
    for (var key in finalArray) {
        var value = finalArray[key];
        // console.log(key, value);
        scheduleObj[key] = value;
    }
    console.log(scheduleObj);


    var hash = Object.create(null);
    var result = Object.create(null);

    Object.keys(scheduleObj).forEach(k => {
        var grp = scheduleObj[k];

        (grp in hash) ? hash[grp].push(k) : (hash[grp] = [k]);
    });

    for (key in hash) {
        if (hash[key].length > 1) {
            result[key] = hash[key].toString();
        }
    }

    console.log(hash);
    console.log('hash sa taas');


    var finalScheduleArray = [];
    // loop hash
    Object.keys(hash).forEach(function(key) {
        // console.log(key, result[key]);
        console.log(key, hash[key]);
        var my_key = key;
        var my_value = hash[key];


        if (my_value.length == 1) {
            finalScheduleArray[my_value] = my_key;
        } else {

            var rangeArray = [];
            for (i = 0; i < my_value.length; i++) {
                console.log(my_value[i]);
                rangeArray.push(my_value[i]);

            }
            console.log(rangeArray);

            var firstItem = rangeArray[0];
            var lastItem = rangeArray[rangeArray.length-1];
            finalScheduleArray[firstItem + ' - ' + lastItem] = my_key;
        }

    });

    console.log(finalScheduleArray);
    console.log('finalScheduleArray ----------');
    return 'zzz';
}

$(function() {
    // var schedule = 'Mon 4:00am - 9:00pm, Tue 6:00am - 9:30pm, Wed - Fri 5:30am - 9:30pm, Sat 8:00am - 3:00pm, Sun Closed'; // MALI Wed - Fri
    // var schedule = 'Mon 4:30am - 9:30pm, Tue 5:30am - 9:30pm, Wed 5:00am - 9:30pm, Thu - Sat 8:00am - 3:00pm, Sun Closed'; // tama Thu - Sat
    var schedule = 'Mon 3:00am - 9:00pm, Tue - Thu 5:30am - 9:30pm, Fri 7:15am - 10:00pm, Sat 8:00am - 3:00pm, Sun Closed'; // MALI Tue - Thu
    console.log(reorderSchedule(schedule));
});

最佳答案

以下内容可能会起作用,它不会删除“ Sun”,不确定您是否只想删除这一天,或者因为它刚好关闭。



const week = [
  'Sun',
  'Mon',
  'Tue',
  'Wed',
  'Thu',
  'Fri',
  'Sat',
];
const withDayPair = (item) => {
  //add [dayOneIndex, dayTwoIndex] to value
  const parts = item
    .split('-')
    .map((s) => s.trim())
    .map((s) => s.substr(0, 3));
  if (parts.length > 2) {
    //this value has day range (for example Mon - Wed)
    return [
      parts.slice(0, 2).map((p) => week.indexOf(p)),
      item,
    ];
  }
  return [[week.indexOf(parts[0]), -1], item]; //only one day, set second day to -1
};
const assignValue = (current) => ([[a, b], item]) => {
  //assign a value for sorting. If both values are smaller than current (earlier in the week)
  //  then add 10 to the index of the value in the array
  const val = a < current && b < current ? a + 10 : a;
  return [val, item];
};
const sortWith = (str, day) =>
  str
    .split(',')
    .map((s) => s.trim())
    .filter((s) => !s.startsWith('Sun'))//remove sunday
    //having a string representing days of week is not the best way to represent
    //  the data types used in your application
    .map(withDayPair)
    //now the values will be [[1,3],'Mon - Wed ....']
    .map(assignValue(week.indexOf(day))) //the [1,3] part is replaced with one number
    .sort(([a], [b]) => a - b) //sorting on that number
    .map(([_, item]) => item); //remove the number
const testVal =
  'Mon - Tue 5:30am - 9:30pm, Wed 6:30am - 4:00pm, Thu - Sat 8:00am - 3:00pm, Sun Closed';
console.log(
  //test for every day in the week
  ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    .map(
      (day) =>
        `Day:${day} == ${sortWith(testVal, day).join(',')}`,
    )
    .join('\n'),
);

关于javascript - Javascript Array排序并制定与日数范围相同的计划,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52426210/

10-09 21:15