周日,周一,...,周六等工作日的排序对象[重复]
Posted
技术标签:
【中文标题】周日,周一,...,周六等工作日的排序对象[重复]【英文标题】:Sort object of weekdays like Sunday, Monday, ..., Saturday [duplicate] 【发布时间】:2016-03-08 02:40:43 【问题描述】:如何按工作日对工作日的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;
);
谢谢。
【问题讨论】:
您能否发布您的代码以向我们展示您已经尝试过的内容,并解释它有什么问题? 我在控制台上试过,但无法完成。 只是为了为您指明正确的方向。 Moment.JS 让您提供一个像这样的对象并获得生成的日期编号moment("Friday", "e").weekday()
。将它与 lodash 的 sortBy
之类的东西结合起来可以很容易地工作。只是一个建议。
它是一个对象,不能按属性名排序,规范中不保证顺序:***.com/questions/5525795/…
这不是重复的!如果我们通过将工作日视为文本来对它们进行排序,那么排序顺序将是错误的。 ***.com/questions/1069666/… 不回答这个问题。
【参考方案1】:
就像我在评论中所说,您无法对对象进行排序,但如果您可以将数据格式更改为数组,则可以使用 [].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>");
【讨论】:
谢谢。但我想根据键名进行排序。请检查输入对象。 我更新了我的答案,但使用代码需要您自担风险。 According to MDN,比较函数应该返回一个负数,一个正数,或者0,不是真假。 我添加了一个编辑来更新函数以返回值 (if (sorter[day1] > sorter[day2]) return 1; if (sorter[day2] > sorter[day1]) return -1; return 0;) 以便正确排序。【参考方案2】:就像使用 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);
这会返回:
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/
【讨论】:
谢谢。试过了。它不工作 @VishalRajole 它在什么方面不起作用? 它返回相同的对象,如未排序。我没有得到你的意思来消除时刻依赖。你能详细说明一下吗? @VishalRajole 我已经更新了没有 MomentJS 的工作答案 谢谢。是的,它正在小提琴中工作。但是相同的代码在 chrome 中不起作用。以上是关于周日,周一,...,周六等工作日的排序对象[重复]的主要内容,如果未能解决你的问题,请参考以下文章
sql 查找下周一/周二/周三/周四/周五/周六/周日的日期
每月第 5 个工作日的 TimeTrigger - 计划的 Web 作业 Azure