使用Lodash转换此嵌套的json数据

dev*_*abe 3 javascript json nested underscore.js lodash

我是lodash的新手.我有这种json数据.我已经有90-120行代码解析它,对它进行一些计算,并创建新的json.它正在成为一场噩梦.

我想问一下如何使用lodash的方法来改变这个json.

http://pastebin.com/BjBLwJDA

[
    {
        "dates": [
            {
                "datetime": "2014-08-26",
                "deviceModels": [
                    {
                        "deviceModel": "Canon450MX",
                        "devices": [
                            {
                                "deviceModel": "Canon450MX",
                                "inchesPrinted": 10,
                                "serialNum" : "111"
                            },
                            {
                                "deviceModel": "Canon450MX",
                                "inchesPrinted": 10,
                                "serialNum" : "222"
                            },
                            {
                                "deviceModel": "Canon450MX",
                                "inchesPrinted": 10,
                                "serialNum" : "333"
                            }
                        ]
                    },
                    {
                        "deviceModel": "HPDeskjet",
                        "devices": [
                            {
                                "deviceModel": "HPDeskjet",
                                "inchesPrinted": 20,
                                "serialNum" : "444"
                            },
                            {
                                "deviceModel": "HPDeskjet",
                                "inchesPrinted": 20,
                                "serialNum" : "555"
                            }
                        ]
                    }
                ]
            },
            {
                "datetime": "2014-08-27",
                "deviceModels": [
                    {
                        "deviceModel": "Canon450MX",
                        "devices": [
                            {
                                "deviceModel": "Canon450MX",
                                "inchesPrinted": 5,
                                "serialNum" : "111"
                            },
                            {
                                "deviceModel": "Canon450MX",
                                "inchesPrinted": 25,
                                "serialNum" : "222"
                            },
                            {
                                "deviceModel": "Canon450MX",
                                "inchesPrinted": 15,
                                "serialNum" : "333"
                            }
                        ]
                    },
                    {
                        "deviceModel": "HPDeskjet",
                        "devices": [
                            {
                                "deviceModel": "HPDeskjet",
                                "inchesPrinted": 10,
                                "serialNum" : "444"
                            },
                            {
                                "deviceModel": "gx420d",
                                "inchesPrinted": 20,
                                "serialNum" : "555"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

我希望新的输出json是这样的:

[
    { date : 2014-08-26, deviceModel : 'Canon450MX', totalInchesPrinted : 30 },
    { date : 2014-08-26, deviceModel : 'HPDeskJet', totalInchesPrinted : 40 },
    { date : 2014-08-27, deviceModel : 'Canon450MX', totalInchesPrinted : 45 },
    { date : 2014-08-27, deviceModel : 'HPDeskJet', totalInchesPrinted : 30 }
]
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

谢谢!

Gru*_*nny 11

这应该将数据转换为所需的形式:

    var sumInchesPrinted = function(total, device){
        return total + device.inchesPrinted
    }

    var transformDeviceModels = function(dates){
        return _.map(dates.deviceModels, function(deviceModel){
            return {
                date: dates.datetime,
                deviceModel: deviceModel.deviceModel,
                totalInchesPrinted: _.reduce(deviceModel.devices, sumInchesPrinted, 0)
            }
        });
    };

    var data = _.chain(list[0].dates)
        .map(transformDeviceModels)
        .flatten()
        .value();
Run Code Online (Sandbox Code Playgroud)