This question already has answers here:
Sort Json Array with LoDash
                            
                                (2个答案)
                            
                    
                3年前关闭。
        

    

 - const Employers=[
           {employerName:"A1",employees:[{employeeName:"EA1"},{employeeName:"EA2"}]},
           {employerName:"B1",employees:[{employeeName:"EB1"},{employeeName:"EB2"}]}
           ]


我的问题是如何按employeeName排序雇主。

最佳答案

首先从排序的员工排序

_.sortBy(Employers, function(emp) {
    return _.chain(emp)
        .get('employees')
        .sortBy('employeeName')
        .map('employeeName')
        .first()
        .value();
});

09-19 01:23