【发布时间】:2017-03-07 15:50:06
【问题描述】:
我有以下 javascript 对象:
var appointments = [];
appointments.push(
{ id: '101',
status: 'accepted',
appointmentTimes: [ { from: '2016-10-28 12:00', to: '2016-10-28 13:00' } ] },
{ id: '102',
status: 'pending',
appointmentTimes: [ { from: '2016-10-24 12:00', to: '2016-10-24 13:00' },
{ from: '2016-10-24 15:00', to: '2016-10-24 16:00' } ] });
我想使用appointmentTimes 数组中的第一项使用from 属性对数组对象进行排序,以便每个对象按日期升序显示。
所以在上面的例子中,id 为 102 的对象会出现在列表的首位。请注意,约会时间数组中的每个项目都已按升序排列。
我尝试了以下方法,但它不起作用:
_.sortBy(appointments, function(appointments) {
return appointments.appointmentTimes.from;
});
【问题讨论】:
-
使用返回约会.appointmentTimes[0].from;
-
@Vanojx1 谢谢,我设法弄清楚了自己,但还是谢谢。
标签: javascript arrays sorting underscore.js