Edwin Bermejo
Edwin Bermejo

Reputation: 442

Using lodash .groupBy. How to group object inside an object

I'm using lodash's _.groupBy to come up with the following data:

{
   "Generic Drugs":[
                      {
                          itemDes: "Dulcolax",
                          itemGeneric: "Bisacodyl",
                          price: '10'
                      },
                      {
                          itemDes: "Celine",
                          itemGeneric: "Ascorbic Acid",
                          price: '10'
                      },
                          {
                          itemDes: "Vitamic C",
                          itemGeneric: "Ascorbic Acid",
                          price: '10'
                      }
                   ],
  "None-generic Drugs" : [
                           {
                             itemDes: "test 1",
                             itemGeneric: "",
                             price: '10'
                           },
                           {
                             itemDes: "test 2",
                             itemGeneric: "",
                             price: '10'
                           }                                 
                   ]
}

I wanted to group the objects in Generic Drugs object by the property itemGeneric to come up an output like this:

{
   "Generic Drugs":[
                      "Ascorbic Acid" : [  
                                          { itemDes: "Celine" }, 
                                          { itemDesc: "Vitamin C" }
                                        ],
                      "Bisacodyl" : [
                                       { itemDes: "Dolculax" } 
                                    ]
  "None-generic Drugs" : [
                           {
                             itemDes: "test 1",
                             itemGeneric: "",
                             price: '10'
                           },
                           {
                             itemDes: "test 2",
                             itemGeneric: "",
                             price: '10'
                           }                                 
                   ]
}

Please somebody help me with this problem. Thanks for the answers. :)

Upvotes: 0

Views: 11155

Answers (1)

Ori Drori
Ori Drori

Reputation: 192287

Use _.mapValues() on the grouped data, and group each value by the 'itemGeneric':

_.mapValues(grouped, function(group, key) {
      return key === 'Generic Drugs' ? _.groupBy(group, 'itemGeneric') : group;
});

var grouped = {
  "Generic Drugs": [{
    itemDes: "Dulcolax",
    itemGeneric: "Bisacodyl",
    price: '10'
  }, {
    itemDes: "Celine",
    itemGeneric: "Ascorbic Acid",
    price: '10'
  }, {
    itemDes: "Vitamic C",
    itemGeneric: "Ascorbic Acid",
    price: '10'
  }],
  "None-generic Drugs": [{
    itemDes: "test 1",
    itemGeneric: "",
    price: '10'
  }, {
    itemDes: "test 2",
    itemGeneric: "",
    price: '10'
  }]
};

var result = _.mapValues(grouped, function(group, key) {
      return key === 'Generic Drugs' ? _.groupBy(group, 'itemGeneric') : group;
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>

If the original data item is something like this:

  {
      type: "Generic Drugs",
      itemDes: "Dulcolax",
      itemGeneric: "Bisacodyl",
      price: '10'
  }

You can do:

_(originalData).groupBy('type').mapValues(grouped, function(group, key) {
    return key === 'Generic Drugs' ? _.groupBy(group, 'itemGeneric') : group;
});

Upvotes: 3

Related Questions