如何在 lodash _.countBy 之后排序或排序?

问题描述 投票:0回答:2

我已经完成了 lodash

_.countBy
并返回了一个像这样的对象......

sponsorCounts = { 300051: 2, 300055: 1, 300064: 6 }

现在我想按降序值进行排序/排序,但我发现的所有示例都涉及将数据推送到单独的数组中进行排序...

var sortable = [];
for (var sponsor in sponsorCounts)
     sortable.push([sponsor, sponsorCounts[sponsor]])
     sortable.sort(
         function(a, b) {
              return b[1] - a[1]
         }
     )

这给了我一个数组......但我想将它保留在原始对象中,这样我就可以得到它......

sponsorCounts = { 300064: 6, 300051: 2, 300055: 1  }
javascript sorting lodash
2个回答
0
投票

您无法对 JavaScript 对象进行排序。他们不保证订单。你必须使用类似数组的东西。对不起!


0
投票

一种解决方案是使用 Arquero

aq.from(sponsorCounts)
.orderby(aq.desc("value"))
.view()

参见https://uwdata.github.io/arquero/api/

© www.soinside.com 2019 - 2024. All rights reserved.