Grouping by using lodash(使用Lotash进行分组)
本文介绍了使用Lotash进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象数组,如下所示:
var data = [
{
"count": 1,
"make": "ALFA ROMEO",
"model": "GIULIETTA DIESEL - 2010"
},
{
"count": 2,
"make": "AUDI",
"model": "A1 DIESEL"
},
{
"count": 1,
"make": "AUDI",
"model": "A1 SPORTBACK DIESEL"
},
{
"count": 2,
"make": "AUDI",
"model": "A3 DIESEL - 2012"
},
{
"count": 3,
"make": "Volkswagen",
"model": "Golf"
},
{
"count": 3,
"make": "Ford",
"model": "Escord"
},
{
"count": 2,
"make": "Opel",
"model": "Zafira"
}
]
我要group by
make
,然后获得计数最高的三个Make和要显示为其他的睡觉。
例如,我想获得:
var result = [
{
"brand": "Audi",
"count": 5
},
{
"brand": "Volkswagen",
"count": 3
},
{
"brand": "Ford",
"count": 3
},
{
"brand": "Other",
"count": 3
}
]
我不知道怎么开始。有什么帮助吗?
推荐答案
使用LOCASH
创建一个旅店连锁店。按make使用_.groupBy()
,然后使用_.map()
结果和_.sumBy()
属性。使用_.values()
转换回数组,使用_.orderBy()
降序排序,然后使用_.value()
完成链。Split将结果分成两个数组,并使用Reduce将第二个数组(低集合计数)相加:
var data = [{"count":1,"make":"ALFA ROMEO","model":"GIULIETTA DIESEL - 2010"},{"count":2,"make":"AUDI","model":"A1 DIESEL"},{"count":1,"make":"AUDI","model":"A1 SPORTBACK DIESEL"},{"count":2,"make":"AUDI","model":"A3 DIESEL - 2012"},{"count":3,"make":"Volkswagen","model":"Golf"},{"count":3,"make":"Ford","model":"Escord"},{"count":2,"make":"Opel","model":"Zafira"}];
var counts = _(data)
.groupBy('make')
.map(function(g, key) { return {
make: key,
count: _.sumBy(g, 'count')
};})
.values()
.orderBy('count', 'desc')
.value();
var result = counts.slice(0, 3).concat({
brand: 'other',
count: counts.slice(3).reduce(function(s, { count }) { return s + count; }, 0)
})
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
使用ES6
使用Array#reduce迭代,将所有make count值收集到Map中的一个对象,然后获取map values iterator和spread以获得数组,并进行降序排序。Split将结果分成两个数组,并使用Reduce将第二个数组(低集合计数)相加:
const data = [{"count":1,"make":"ALFA ROMEO","model":"GIULIETTA DIESEL - 2010"},{"count":2,"make":"AUDI","model":"A1 DIESEL"},{"count":1,"make":"AUDI","model":"A1 SPORTBACK DIESEL"},{"count":2,"make":"AUDI","model":"A3 DIESEL - 2012"},{"count":3,"make":"Volkswagen","model":"Golf"},{"count":3,"make":"Ford","model":"Escord"},{"count":2,"make":"Opel","model":"Zafira"}];
const counts = [...data.reduce((m, { make, count }) => {
const item = m.get(make) || { make, count: 0 };
item.count += count;
return m.set(make, item);
}, new Map()).values()].sort((a, b) => b.count - a.count);
const result = counts.slice(0, 3).concat({
brand: 'other',
count: counts.slice(3).reduce((s, { count }) => s + count, 0)
})
console.log(result);
这篇关于使用Lotash进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用Lotash进行分组
基础教程推荐
猜你喜欢
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01