Javascript 总结,;用Lodash分组
我是Lodash的新手,我正在尝试使用GROUPBY作为SQL执行复杂的求和,但我没有找到任何解决方案。我曾尝试使用/组合多个Lodash函数,但没有成功 我的要求是这样的。我有一个JSON响应:Javascript 总结,;用Lodash分组,javascript,highcharts,lodash,Javascript,Highcharts,Lodash,我是Lodash的新手,我正在尝试使用GROUPBY作为SQL执行复杂的求和,但我没有找到任何解决方案。我曾尝试使用/组合多个Lodash函数,但没有成功 我的要求是这样的。我有一个JSON响应: input = [{"quantity":1067,"gross_revenue":4094.2,"date":"03","company":"Cat1","product":"Car"}, {"quantity":106,"gross_revenue":409,"date":"02","compa
input =
[{"quantity":1067,"gross_revenue":4094.2,"date":"03","company":"Cat1","product":"Car"},
{"quantity":106,"gross_revenue":409,"date":"02","company":"Cat2","product":"Car"},
{"quantity":106,"gross_revenue":85,"date":"03","company":"Cat2","product":"House"},
{"quantity":106,"gross_revenue":100,"date":"02","company":"Cat3","product":"House"},
{"quantity":20,"gross_revenue":150,"date":"03","company":"Cat5","product":"Technology"},
{"quantity":40,"gross_revenue":100,"date":"01","company":"Cat5","product":"Technology"},
{"quantity":20,"gross_revenue":15,"date":"01","company":"Cat5","product":"Car"},
{"quantity":20,"gross_revenue":18,"date":"01","company":"Cat5","product":"House"},
{"quantity":20,"gross_revenue":2,"date":"01","company":"Cat2","product":"House"},
{"quantity":20,"gross_revenue":25,"date":"01","company":"Cat3","product":"House"}]
我需要生成如下结果来填充HighChart系列:
[{ name: 'Car', data: [15, 409, 4094.2] },
{ name: 'House', data:[45, 100, 85] },
{ name: 'Techonology', data:[100, null, 150] }]
这些值是由以下因素产生的:
谢谢。这里有一种方法——当然不是唯一的解决方案
var输入=[
{“数量”:1067,“总收入”:4094.2,“日期”:“03”,“公司”:“Cat1”,“产品”:“Car”},
{“数量”:106,“总收入”:409,“日期”:“02”,“公司”:“Cat2”,“产品”:“Car”},
{“数量”:106,“总收入”:85,“日期”:“03”,“公司”:“Cat2”,“产品”:“房屋”},
{“数量”:106,“总收入”:100,“日期”:“02”,“公司”:“Cat3”,“产品”:“房屋”},
{“数量”:20,“总收入”:150,“日期”:“03”,“公司”:“Cat5”,“产品”:“技术”},
{“数量”:40,“总收入”:100,“日期”:“01”,“公司”:“Cat5”,“产品”:“技术”},
{“数量”:20,“总收入”:15,“日期”:“01”,“公司”:“Cat5”,“产品”:“Car”},
{“数量”:20,“总收入”:18,“日期”:“01”,“公司”:“Cat5”,“产品”:“房屋”},
{“数量”:20,“总收入”:2,“日期”:“01”,“公司”:“Cat2”,“产品”:“房屋”},
{“数量”:20,“总收入”:25,“日期”:“01”,“公司”:“Cat3”,“产品”:“房屋”}
];
var结果=[];
var groupedByProduct=uu.groupBy(输入,“产品”);
//获取一组唯一的日期
变量日期=q.uniq(q.map(输入,'date'));
//对于每个产品,执行聚合
_.forEach(分组副产品、函数(值、键){
//为每个日期初始化数据数组
数据=[];
对于(变量i=0;i
非常好@rphv感谢您的帮助。它是一种解决方案的组合,您也可以使用阵列。我很困惑,因为我试图只使用Lodash。现在清楚了!2年后,这救了我的命