javascript 对工作日的对象进行排序,例如周日、周一、...、周六
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34066752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort object of weekdays like Sunday, Monday, ..., Saturday
提问by Vishal Rajole
How to sort Javascript object of week days by weekdays i.e Here, is JSON format of 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"]}
where key is day i.e."Wednesday". I want to sort it based on order of weekdays i.e Sunday, Monday, Tuesday etc
关键是一天,即“星期三”。我想根据工作日的顺序对其进行排序,即星期日、星期一、星期二等
Expected Output is:
预期输出为:
{ "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"]}
this is what i was trying.
这就是我正在尝试的。
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;
});
Thanks.
谢谢。
回答by Shanoor
Like I said in my comment, you can't sort an object but if you can change your data's format to have an array, you can easily sort it by using [].sort
就像我在评论中所说的那样,您无法对对象进行排序,但是如果您可以更改数据的格式以拥有数组,则可以使用以下方法轻松对其进行排序 [].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>");
edit
编辑
To "order" your object's keys by day of the week, again order is not guaranteed, use at your own risk
按星期几“订购”对象的键,再次订购不能保证,使用风险自负
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>");
回答by SlashmanX
Just as a proof of concept using ES6, this should work Requires MomentJS:
就像使用 ES6 的概念证明一样,这应该可以工作 Requires 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);
This returns:
这将返回:
{
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' ]
}
Note: This is mainly a proof of concept to show it is possible, there are issues such as MomentJS treating Sunday as day 0 (based on user locale etc). You could easily write your own mapping of weekdays to int (Monday -> 0, Tuesday -> 1) and remove the MomentJS dependency.
注意:这主要是一个概念证明以证明它是可能的,存在诸如 MomentJS 将星期日视为第 0 天(基于用户区域设置等)的问题。您可以轻松地编写自己的工作日到 int(星期一 -> 0,星期二 -> 1)的映射并删除 MomentJS 依赖项。
EDIT: Expanding on above:
编辑:在上面扩展:
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
Then change the sort function to
然后将排序函数更改为
return days.indexOf(a) > days.indexOf(b)
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);
Example: http://www.es6fiddle.net/ihqg46kp/