Search code examples
javascriptjsonnestedchaininglodash

Can't achieve JSON structure using Lodash


I'm still having a hard time trying to figure out which methods to use. I'm playing with the earlier lodash solution that was given to me so I can learn how Lodash is working. What's currently happening is that the output is becoming an object and the deviceModels are turning into keys. I was expecting this kind of output

[ { "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 } ] } ]

But the output is being generated like this

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

Here is my JS Fiddle - http://jsfiddle.net/ohgenLr5/2/

Also, I would like to know how I can add overallTotal and deviceCount(shown above) based on unique serial number for that day.

Any help would be greatly appreciated!

Thanks!


Solution

  • You just need to do an additional map after groupBy to get the structure you want, using reduce to get the sum of total inches printed...

    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