我有以下 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' } ] });
我想使用 from
属性使用 appointmentTimes
数组中的第一项对数组对象进行排序,以便每个对象按日期升序显示。
因此,在上面的示例中,id 为 102 的对象将首先出现在列表中。请注意,appointmentTimes 数组中的每一项都已按升序排列。
我已尝试以下方法,但不起作用:
_.sortBy(appointments, function(appointments) {
return appointments.appointmentTimes.from;
});
最佳答案
设法修复:
_.sortBy(appointments, function(appointment) {
return appointment.appointmentTimes[0].from;
});
关于javascript - Underscore.js - 如何对嵌套的对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242627/