对工作日的对象进行排序,如周日、周一、..,星期六

Sort object of weekdays like Sunday, Monday, ..., Saturday

本文关键字:周日 周一 星期六 排序 工作日 对象      更新时间:2023-09-26

如何按工作日对Javascript对象进行排序,即这里是对象的JSON格式:

 {"Friday":["5:00pm to 12:00am"] ,"Wednesday":["5:00pm to 11:00pm"],"Sunday":["11:00am to 11:00pm"], "Thursday":["5:00pm to 11:00pm"],"Saturday":["11:00am to 12:00am"]}

其中关键是日期,即"周三"。我想根据工作日的顺序进行排序,即周日、周一、周二等

预期输出为:

{ "Wednesday":["5:00pm to 11:00pm"], "Thursday":["5:00pm to 11:00pm"], "Friday":["5:00pm to 12:00am"], "Saturday":["11:00am to 12:00am"], "Sunday":["11:00am to 11:00pm"]}

这就是我所尝试的。

var keys = {}; 
Object.keys(scope.daySpecificHours)
.map(function (k) { return [k, scope.daySpecificHours[k]]; })
.sort(function (a, b) {
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;
return 0;
})
.forEach(function (d) {
scope.daySpecificHours[d[0]] = d;
});

谢谢。

正如我在评论中所说,您不能对对象进行排序,但如果您可以更改数据的格式以具有数组,则可以使用[].sort 轻松地对其进行排序

let data = [
  { day: "Friday", hours: ["5:00pm to 12:00am"] },
  { day: "Wednesday", hours: ["5:00pm to 11:00pm"] },
  { day: "Sunday", hours: ["11:00am to 11:00pm"] },
  { day: "Thursday", hours: ["5:00pm to 11:00pm"] },
  { day: "Saturday", hours: ["11:00am to 12:00am"] }
];
const sorter = {
  // "sunday": 0, // << if sunday is first day of week
  "monday": 1,
  "tuesday": 2,
  "wednesday": 3,
  "thursday": 4,
  "friday": 5,
  "saturday": 6,
  "sunday": 7
}
data.sort(function sortByDay(a, b) {
  let day1 = a.day.toLowerCase();
  let day2 = b.day.toLowerCase();
  return sorter[day1] - sorter[day2];
});
console.log(data);
document.write("<pre>" + JSON.stringify(data, null, 3) + "</pre>");

编辑

要在一周中的某一天"订购"对象的密钥,订单不受保证,请使用,风险自负

let data = {
  "Friday": ["5:00pm to 12:00am"],
  "Wednesday": ["5:00pm to 11:00pm"],
  "Sunday": ["11:00am to 11:00pm"],
  "Thursday": ["5:00pm to 11:00pm"],
  "Saturday": ["11:00am to 12:00am"]
};
const sorter = {
  "monday": 1,
  "tuesday": 2,
  "wednesday": 3,
  "thursday": 4,
  "friday": 5,
  "saturday": 6,
  "sunday": 7
};
let tmp = [];
Object.keys(data).forEach(function(key) {
  let value = data[key];
  let index = sorter[key.toLowerCase()];
  tmp[index] = {
    key: key,
    value: value
  };
});
let orderedData = {};
tmp.forEach(function(obj) {
  orderedData[obj.key] = obj.value;
});
console.log(orderedData);
document.write("<pre>" + JSON.stringify(orderedData, null, 3) + "</pre>");

正如使用ES6进行概念验证一样,这应该有效。需要MomentJS:

const unordered = {"Friday":["5:00pm to 12:00am"] ,"Wednesday":["5:00pm to 11:00pm"],"Sunday":["11:00am to 11:00pm"], "Thursday":["5:00pm to 11:00pm"],"Saturday":["11:00am to 12:00am"]};
const ordered = {};
Object.keys(unordered).sort(function (a, b) {
    return moment(a, 'ddd dddd').weekday() > moment(b, 'ddd dddd').weekday();
}).forEach(function(key) {
    ordered[key] = unordered[key];
});
console.log(ordered);

返回:

{ 
    Sunday: [ '11:00am to 11:00pm' ],
    Wednesday: [ '5:00pm to 11:00pm' ],
    Thursday: [ '5:00pm to 11:00pm' ],
    Friday: [ '5:00pm to 12:00am' ],
    Saturday: [ '11:00am to 12:00am' ] 
}

注意:这主要是一个概念验证,以表明这是可能的,存在MomentJS将周日视为第0天(基于用户区域设置等)等问题。您可以轻松地编写自己的工作日到int的映射(星期一->0,星期二->1),并删除MomentJS依赖项。

编辑:扩展到上面:

var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

然后将排序功能更改为

return days.indexOf(a) > days.indexOf(b)

const unordered = {"Friday":["5:00pm to 12:00am"] ,"Wednesday":["5:00pm to 11:00pm"],"Sunday":["11:00am to 11:00pm"], "Thursday":["5:00pm to 11:00pm"],"Saturday":["11:00am to 12:00am"]};
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const ordered = {};
Object.keys(unordered).sort(function (a, b) {
    return days.indexOf(a) > days.indexOf(b);
}).forEach(function(key) {
    ordered[key] = unordered[key];
});
console.log(ordered);

示例:http://www.es6fiddle.net/ihqg46kp/