【问题标题】:Underscore.js - How to sortby a nested array of objectsUnderscore.js - 如何按嵌套的对象数组排序
【发布时间】: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


【解决方案1】:

设法修复:

_.sortBy(appointments, function(appointment) { 
    return appointment.appointmentTimes[0].from; 
});

【讨论】:

    猜你喜欢
    • 2013-11-06
    • 2015-11-06
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2019-10-27
    • 2015-11-22
    • 2012-03-06
    相关资源
    最近更新 更多