1

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

2 Answers 2

1

I think this can be the Underscore version of my previous implementation. BTW here is the jsfiddle if you wanna play. ;)

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  "}];

var getServiceInfo = group => _.chain(group)
  .map((v, k) => {
    var result = {
      info: v.info,
      note: v.note
    };
    return result;
  });


var getService = group => _.chain(group)
  .groupBy('serviceName').map((v, k) => {
    var result = {
      name: k,
      details: getServiceInfo(v)
    };
    return result;
  });


var getCoverage = group => _.chain(group)
  .groupBy('coverageName').map((v, k) => {
    var result = {
      name: k,
      service: getService(v)
    };
    return result;
  });


var wrappedObject = _.chain(regions)
  .groupBy('plan').map((v, k) => {
    var result = {
      plan: k,
      coverage: getCoverage(v)
    }
    return result;
  });

var result = JSON.parse(JSON.stringify(wrappedObject));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

Sign up to request clarification or add additional context in comments.

4 Comments

mortezaT, this is pretty cool. This works as I want, but since I am ES6 challenged, I tried to convert this to ES5 something like this var getServiceInfo = function(group)= _.chain(group) .map((v, k) { var result = { info: v.info, note: v.note }; return result; }); but running into issues. Do you mind helping with this?
@JBone if you try to change it to javascript es5 then you should add function keyword and curly braces as well function (group) { _.chain(group).map(function (v, k) { var result = { info: v.info, note: v.note }; return result; }); }
Thank you mortezaT. I accepted your answer.Also, I tried to give multiple 'upvotes' but it allowed me only one. Again, thanks. I am trying to get more comfortable with underscorejs etc but now I know whom to talk to if I run into issues :)
@JBone the rules is one upvote per user on others answers or question and comments like you last comments will flagged as too chatty or not constructive so there is no need of thanking.
1

I don't know this is the true answer or not.

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  "
}];

var outObj = {};

regions.forEach(rg => {
  var plan = rg.plan,
    coverage = rg.coverageName,
    service = rg.serviceName
  if (!outObj[plan])
    outObj[plan] = {
      plan: plan,
      coverage: {}
    };
  plan = outObj[plan];
  if (!plan.coverage[coverage])
    plan.coverage[coverage] = {
      name: coverage,
      service: {}
    };
  coverage = plan.coverage[coverage];
  if (!coverage.service[service])
    coverage.service[service] = [];
  service = coverage.service[service];
  service.push({
    info: rg.info,
    note: rg.note
  });
});

function _getServices(coverage) {

  return Object.keys(coverage.service).map(srvName => ({
    name: srvName,
    details: coverage[srvName]
  }));
}

function _getCoverage(plan) {
  return Object.keys(plan.coverage).map(cvrName => {
    var coverage = plan.coverage[cvrName]
    return {
      name: cvrName,
      service: _getServices(coverage)
    };
  });
}

var out = Object.keys(outObj).map(planName => {
  var plan = outObj[planName];
  return {
    name: planName,
    coverage: _getCoverage(plan)
  }
});

console.log(out);

1 Comment

mortezaT, thanks for the work. I though of doing this way, but I realized may be 'underscore' will give me compact code that will reduce my error chances. see if you can help me with the code that I wrote in my original question (that uses underscore's chain, and aggregate methods). If I dont find an answer the way I am intending, I accept yours and will up vote again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.