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

时间:2014-08-29 06:22:18

标签: 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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

我希望新的输出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 }
]

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分: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();