2014-09-01 53 views
0

我仍然很難找出使用哪種方法。我正在玩給我的早先lodash解決方案,所以我可以學習Lodash是如何工作的。目前發生的事情是,輸出正在成爲一個對象,而設備模型正在成爲關鍵。我期待這種輸出無法使用Lodash實現JSON結構

[ { "deviceModel": "Canon450MX", "overallTotal": 75, "deviceCount": 3, "dates": [ { "date": "2014-09-01T05:00:00Z", "deviceModel": "Canon450MX", // This can be removed "totalInchesPrinted": 30 }, { "date": "2014-09-01T06:00:00Z", "deviceModel": "Canon450MX", // This can be removed "totalInchesPrinted": 180 }, { "date": "2014-09-01T06:00:00Z", "deviceModel": "Canon450MX", // This can be removed "totalInchesPrinted": 45 } ] }, { "deviceModel": "Canon9000ST", "overallTotal": 645, "deviceCount": 3, "dates": [ { "date": "2014-09-01T06:00:00Z", "deviceModel": "Canon9000ST", // This can be removed "totalInchesPrinted": 600 }, { "date": "2014-09-01T06:00:00Z", "deviceModel": "Canon9000ST", // This can be removed "totalInchesPrinted": 45 } ] }, { "deviceModel": "HPDeskjet", "overallTotal": 76, "deviceCount": 3, "dates": [ { "date": "2014-09-01T05:00:00Z", "deviceModel": "HPDeskjet", // This can be removed "totalInchesPrinted": 40 }, { "date": "2014-09-01T06:00:00Z", "deviceModel": "HPDeskjet", // This can be removed "totalInchesPrinted": 6 }, { "date": "2014-09-01T07:00:00Z", "deviceModel": "HPDeskjet", // This can be removed "totalInchesPrinted": 30 } ] } ]

,但被這樣

{ "Canon450MX": [ { "date": "2014-09-01T05:00:00Z", "deviceModel": "Canon450MX", "totalInchesPrinted": 30 }, { "date": "2014-09-01T06:00:00Z", "deviceModel": "Canon450MX", "totalInchesPrinted": 180 }, { "date": "2014-09-01T07:00:00Z", "deviceModel": "Canon450MX", "totalInchesPrinted": 45 } ], "Canon9000ST": [ { "date": "2014-09-01T06:00:00Z", "deviceModel": "Canon9000ST", "totalInchesPrinted": 600 }, { "date": "2014-09-01T07:00:00Z", "deviceModel": "Canon9000ST", "totalInchesPrinted": 45 } ], "HPDeskjet": [ { "date": "2014-09-01T05:00:00Z", "deviceModel": "HPDeskjet", "totalInchesPrinted": 40 }, { "date": "2014-09-01T06:00:00Z", "deviceModel": "HPDeskjet", "totalInchesPrinted": 6 }, { "date": "2014-09-01T07:00:00Z", "deviceModel": "HPDeskjet", "totalInchesPrinted": 30 } ] }

這裏是我的JS小提琴產生的輸出 - http://jsfiddle.net/ohgenLr5/2/

另外,我想知道我如何根據當天唯一的序列號添加overallTotal和deviceCount(如上所示)。

任何幫助將不勝感激!

謝謝!

+0

的GROUPBY調用似乎是使用deviceModel鍵將列表變成對象的東西。 – 2014-09-01 17:41:07

+0

好的。我應該像這樣再次使用.map嗎? .map(transformDeviceModels).flatten()。sortBy('deviceModel')。map(someOtherFunction) .value(); – devwannabe 2014-09-01 18:09:36

回答

1

你只需要做一個額外的mapgroupBy後能得到你想要的結構,採用reduce拿到打印的全部英寸的總和......

var data = _.chain(list[0].dates) 
    .map(transformDeviceModels) 
    .flatten() 
    .sortBy('deviceModel') 
    .groupBy('deviceModel') 
    .map(function(printers, model) { 
     return { 
      deviceModel: model, 
      overallTotal: _.reduce(printers, function(sum, printer) { 
       return sum + printer.totalInchesPrinted; 
      }, 0), 
      deviceCount: printers.length, 
      dates: printers 
     }; 
    }) 
    .value(); 

Live Demo

+0

我要去看看安東尼。感謝您的幫助。 – devwannabe 2014-09-02 05:46:35

+0

哇!這真是太棒了安東尼!非常感謝!!! :)這將幫助我更快學習Lodash! :) – devwannabe 2014-09-02 05:53:07

+0

我仍然無法達到這個觀點,因爲我的聲望只有3.我剛剛加入了stackoverflow。 – devwannabe 2014-09-02 05:53:58