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
2 Answers
Reset to default 8you 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
版权声明:本文标题:javascript - Lodash orderBy based on property child count - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743777327a2537218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论