2016-04-20 142 views
1

我是Lodash的新手,我試圖用SQL作爲一個複雜的總和,但是我沒有找到任何解決方案。我試圖使用/組合多個Lodash函數而沒有成功。用Lodash總結和分組

我的要求是這樣的。我有一個JSON響應:

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] }] 

這些值與結果:

  1. 製作使用帶標籤的產品組名稱
  2. 基於以下步驟,gen一箇中心提供全方位陣列與標籤數據

    2.1總和基於產品和日期(所有現有的日期)

    2.2包含空值,如果不存在收入總額爲任何現有

    一天的毛收入

    2.3排序結果基於日期總收入,升序

這可能嗎?還是有另一種解決方案呢? 謝謝。

回答

1

下面是做這件事 - 這當然不是唯一的解決辦法...

var input = [ 
 
\t {"quantity":1067,"gross_revenue":4094.2,"date":"03","company":"Cat1","product":"Car"}, 
 
\t {"quantity":106,"gross_revenue":409,"date":"02","company":"Cat2","product":"Car"}, 
 
\t {"quantity":106,"gross_revenue":85,"date":"03","company":"Cat2","product":"House"}, 
 
\t {"quantity":106,"gross_revenue":100,"date":"02","company":"Cat3","product":"House"}, 
 
\t {"quantity":20,"gross_revenue":150,"date":"03","company":"Cat5","product":"Technology"}, 
 
\t {"quantity":40,"gross_revenue":100,"date":"01","company":"Cat5","product":"Technology"}, 
 
\t {"quantity":20,"gross_revenue":15,"date":"01","company":"Cat5","product":"Car"}, 
 
\t {"quantity":20,"gross_revenue":18,"date":"01","company":"Cat5","product":"House"}, 
 
\t {"quantity":20,"gross_revenue":2,"date":"01","company":"Cat2","product":"House"}, 
 
\t {"quantity":20,"gross_revenue":25,"date":"01","company":"Cat3","product":"House"} 
 
]; 
 

 

 
var result = []; 
 

 
var groupedByProduct = _.groupBy(input, "product"); 
 

 
// get the set of unique dates 
 
var dates = _.uniq(_.map(input, 'date')); 
 

 
// for each product, perform the aggregation 
 
_.forEach(groupedByProduct, function(value, key) { 
 
    // initialize the data array for each date 
 
    data = []; 
 
    for (var i = 0; i < dates.length; i++) { 
 
     data.push(null); 
 
    } 
 

 
    // aggregate gross_revenue by date 
 
    _.forEachRight(_.groupBy(groupedByProduct[key], "date"), function(dateValue, dateKey) { 
 
     // use the date as an array index 
 
     data[parseInt(dateKey) - 1] = _.sumBy(dateValue, function(o) { 
 
      return o.gross_revenue 
 
     }); 
 
    }); 
 
    
 
    // push into the result array 
 
    result.push({"name": key, "data": data}); 
 
}); 
 

 
document.getElementById("result").innerHTML = JSON.stringify(result);
<script src="https://cdn.jsdelivr.net/lodash/4.11.1/lodash.min.js"></script> 
 
<pre id="result"></pre>

+0

優秀@rphv感謝您的幫助。在您處理數組時,它也是解決方案的組合。我很困惑,因爲我試圖只使用Lodash。現在很清楚! – georgeos