【问题标题】:Use Lodash to transform this nested json data使用 Lodash 转换这个嵌套的 json 数据
【发布时间】:2014-10-23 03:16:48
【问题描述】:

我是 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 }
]

任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: javascript json nested underscore.js lodash


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了,哇,成功了!!!与我自己的嵌套混乱循环相比,它非常干净,哈哈哈!太感谢了!顺便说一句,我尝试单击向上箭头投票。我收到一条错误消息,提示我需要 15 声望。
    • Gruff Bunny,有没有什么 Lodash 的书可以推荐,或者除了 API 文档之外有很好的教程的网址吗?
    • 有一本很好的书叫做'Functional Javascript' (Michael Fogus),它使用了下划线库,但它不是关于下划线的教程。除此之外,我一次只使用库中的一个函数,直到您对它的工作方式感到满意为止。由于这些编程结构在现实世界中经常出现,因此对 map 和 reduce 的深入了解将使您受益匪浅。
    • 非常感谢您的建议!我会遵守你所说的一切。 :)
    • 顺便说一句,我试图弄清楚 sumInchesPrinted 中的参数是如何被填充的,即使我们只是这样称呼它totalInchesPrinted: _.reduce(deviceModel.devices, sumInchesPrinted, 0) imgur.com/oVOfVM8
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多