admin管理员组

文章数量:1353578

I'm new to lodash. I have this kind of json data. I already have 90-120 lines of code that parses it, does some calculation to it, and creates new json. It's being a nightmare.

I would like to ask help on how I can transform this json using lodash's methods.

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

I would like the new output json to be like this:

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

Any help would be greatly appreciated!

Thanks!

I'm new to lodash. I have this kind of json data. I already have 90-120 lines of code that parses it, does some calculation to it, and creates new json. It's being a nightmare.

I would like to ask help on how I can transform this json using lodash's methods.

http://pastebin./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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

I would like the new output json to be like this:

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

Any help would be greatly appreciated!

Thanks!

Share Improve this question asked Aug 29, 2014 at 6:22 devwannabedevwannabe 3,2108 gold badges44 silver badges82 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

This should transform the data into the required form:

    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();

本文标签: javascriptUse Lodash to transform this nested json dataStack Overflow