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

时间:2015-12-03 13:00:23

标签: javascript angularjs sorting object

如何按工作日对工作日的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"]}

其中key是day,即“Wednesday”。我想根据工作日的顺序对它进行排序,即周日,周一,周二等

预期输出为:

{ "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;
});

感谢。

2 个答案:

答案 0 :(得分:8)

就像我在评论中说的那样,您无法对对象进行排序,但如果您可以将数据格式更改为包含数组,则可以使用[].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>");

修改

要在一周中的某一天“订购”对象的密钥,再次order is not guaranteed,请自担风险使用

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>");

答案 1 :(得分:2)

正如使用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/