【发布时间】:2014-10-23 03:16:48
【问题描述】:
我是 lodash 的新手。我有这种json数据。我已经有 90-120 行代码来解析它,对其进行一些计算,然后创建新的 json。它正在变成一场噩梦。
我想请教如何使用 lodash 的方法转换这个 json。
[
{
"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