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 badges1 Answer
Reset to default 11This 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
版权声明:本文标题:javascript - Use Lodash to transform this nested json data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743935821a2564663.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论