admin管理员组

文章数量:1345895

I would like to count objects of property and orderBy considering the count result.

 var users = [
            { 'user': 'fred',   'age': 48 },
            { 'user': 'barney', 'age': 34 }
        ];

// Sort by `user` in ascending order and by `age` in descending order.
        _.orderBy(users, ['user', 'age'], ['asc', 'desc']);

From the doc, this work but what I want is to have something like :

var users = [
            { 'user': 'barney', 'age': 34 ,Likes: [{objAA},{objBA}]},
            { 'user': 'fred',   'age': 48 ,Likes: [{objBA},{objBB},{objBC}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objDA},{objDA}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objCA}]}
        ];

orderBy Likes count

var users = [
            { 'user': 'fred',   'age': 48 ,Likes: [{objBA},{objBB},{objBC}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objAA},{objBA}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objDA},{objDA}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objCA}]}
        ];

Is this possible using loadh only ?

What I need is something like

_.orderBy(users, ['user.Likes.length'], ['desc']);
_.orderBy(users, ['user.Likes'], ['desc']);

But above code doesn't work but no errors

I would like to use orderBy function to be able after to bine the ordering like

_.orderBy(users, ['user.Likes','user', 'age'], ['desc','asc', 'desc']);

I would like to count objects of property and orderBy considering the count result.

 var users = [
            { 'user': 'fred',   'age': 48 },
            { 'user': 'barney', 'age': 34 }
        ];

// Sort by `user` in ascending order and by `age` in descending order.
        _.orderBy(users, ['user', 'age'], ['asc', 'desc']);

From the doc, this work but what I want is to have something like :

var users = [
            { 'user': 'barney', 'age': 34 ,Likes: [{objAA},{objBA}]},
            { 'user': 'fred',   'age': 48 ,Likes: [{objBA},{objBB},{objBC}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objDA},{objDA}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objCA}]}
        ];

orderBy Likes count

var users = [
            { 'user': 'fred',   'age': 48 ,Likes: [{objBA},{objBB},{objBC}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objAA},{objBA}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objDA},{objDA}]},
            { 'user': 'barney', 'age': 34 ,Likes: [{objCA}]}
        ];

Is this possible using loadh only ?

What I need is something like

_.orderBy(users, ['user.Likes.length'], ['desc']);
_.orderBy(users, ['user.Likes'], ['desc']);

But above code doesn't work but no errors

I would like to use orderBy function to be able after to bine the ordering like

_.orderBy(users, ['user.Likes','user', 'age'], ['desc','asc', 'desc']);
Share Improve this question edited Mar 13, 2016 at 12:18 Aaleks asked Mar 13, 2016 at 12:12 AaleksAaleks 4,3436 gold badges34 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

you may use function iterators like this

 _.orderBy(users, ['user', function (o) {
    return o.likes.length;
 }], ["asc", "asc"])

here's an example

var users = [{
  'user': 'fred',
  'age': 48,
  'likes': [{c:1}, {c:2}, {c:3}]
}, {
  'user': 'barney',
  'age': 34,
  'likes': [{c:1}, {c:2}, {c:3}, {c:3}]
}, {
  'user': 'ammar',
  'age': 25,
  'likes': [{c:1}, {c:2}, {c:3}, {c:3}, {c:1}, {c:2}, {c:3}]
}, {
  'user': 'boston',
  'age': 34,
  'likes': [{c:1}, {c:3}]
}, {
  'user': 'obama',
  'age': 34,
  'likes': [{c:1}]
}, {
  'user': 'barney',
  'age': 34,
  'likes': [{c:1}, {c:2}, {c:3}]
}];


document.write(JSON.stringify(_.orderBy(users, ['user', function (o) {
  return o.likes.length;
}], ["asc", "asc"]), null, 4))
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.6.1/lodash.min.js"></script>

You can pass a function as the iteratee:

_.orderBy(users, user => user.Likes.length, ['desc']);

本文标签: javascriptLodash orderBy based on property child countStack Overflow