[英]Underscore.js - How to sortby a nested array of objects
I have the following javascript object: 我有以下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' } ] });
I want to sort the array objects using the first item in the appointmentTimes
array using the from
property so that each object appears in ascending date order. 我想使用
from
属性使用appointmentTimes
数组中的第一项对数组对象进行排序,以便每个对象按升序日期顺序显示。
So in the above example, object with id 102 would appear first in the list. 因此在上面的示例中,id为102的对象将首先出现在列表中。 Note each item in appointmentTimes array is already in ascending order.
请注意,appointmentTimes数组中的每个项目都已按升序排列。
I've tried the following but it doesnt work: 我尝试了以下但它不起作用:
_.sortBy(appointments, function(appointments) {
return appointments.appointmentTimes.from;
});
Managed to fix: 管理修复:
_.sortBy(appointments, function(appointment) {
return appointment.appointmentTimes[0].from;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.