Sorry for the lengthy post.
Need help with _ aggregation (underscore module) (or anyother better module, or way). I get a ton of JSON objects in an array, and each object have 5 value key pairs:
1) plan, 2) coverageName, 3) serviceName, 4) info, and 5) note.
So I need to aggregate (group by) based on 'plan', 'coverageName', and 'serviceName'.
The result is array of JSON objects in this below format. Basically there can be multiple plans, and each plan can have multiple 'coverage's, and each 'coverage' can have multiple 'service's and each 'service' can have multiple 'details' as show in the JSON structure.
result = [
{
"plan":"Professional",
"coverage": [
{
"name": "Individual",
"service": [
{
"name":"Co-Payment",
"details": [
{
"info"
"note"
},
{
}
] //details aray
},
{
"name":"2nd Payment",
"details": [
{
"info"
"note"
},
{
}
] //details aray
}
] //service array
}
My sample input is this
var regions = [
{
"plan":"Professional",
"coverageName":"Individual",
"serviceName":"Co-Payment",
"info":"In-Network 10 SPECIALIST",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
},
{
"plan":"Professional",
"coverageName":"Non-Individual",
"serviceName":"Co-Insurance",
"info":"In-Network COMBINED OFFICE VISIT INCLUDES OFFICE, OTHER DIAGNOSTIC, THERAPY, SUBSTANCE ABUSE, MENTAL NERVOUS, MATERNITY",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
},
{
"plan":"Professional - Office",
"coverageName":"Individual",
"serviceName":"Co-Insurance",
"info":"In-Network ",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
},
{
"plan":"nonProfessional - visit",
"coverageName":"Individual",
"serviceName":"Co-Insurance",
"info":"In-Network ",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
},
{
"plan":"nonProfessional - visit",
"coverageName":"Individual",
"serviceName":"Co-Insurance",
"info":"In-Network ",
"note":" Certification/Authorization is not required "
}
]
I tried to do this in my logic but I did not make much progress
var _ = require('underscore');
var result = _.chain(regions)
.groupBy("plan")
.map(function(value1, key1) {
return _.chain(value1)
.groupBy("coverageName")
.map(function(value2, key2) {
return _.chain(value2)
.groupBy("serviceName")
.map(function (value3, key3) {
console.log ("\n \n value3 " + JSON.stringify(value3));
}).value()
})
.value();
})
.value();
console.log(JSON.stringify(result));
My output for the above input should look like this
[
{
"plan":"Professional",
"coverage": [
{
"name": "Individual",
"service": [
{
"name":"Co-Payment",
"details": [
{
"info":"In-Network 10 SPECIALIST",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
}
] //details aray
}
] //service array
},
{
"name": "Non-Individual",
"service": [
{
"name":"Co-Insurance",
"details": [
{
"info":"In-Network COMBINED OFFICE VISIT INCLUDES OFFICE, OTHER DIAGNOSTIC, THERAPY, SUBSTANCE ABUSE, MENTAL NERVOUS, MATERNITY",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
}
] //details aray
}] //service array
}] //coverage array
},
{
"plan":"Professional - Office",
"coverage": [{
"name": "Individual",
"service": [{
"name":"Co-Insurance",
"details": [
{
"info":"In-Network ",
"note":" UM PROGRAM INFORMATION IS NOT CURRENTLY AVAILABLE "
}
] //details aray
}] //service array
}]
},
{
"plan":"nonProfessional - visit",
"coverage": [
{
"name": "Individual",
"service": [
{
"name":"Co-Insurance",
"details": [
{
"info":"In-Network ",
"note":" Certification/Authorization is not required "
}
] //details aray
}
] //service array
}
] //coverage
}
]