最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using lodash .groupBy. How to group object inside an object - Stack Overflow

programmeradmin3浏览0评论

I'm using lodash's _.groupBy to e 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 e 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. :)

I'm using lodash's _.groupBy to e 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 e 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. :)

Share Improve this question asked Nov 30, 2016 at 8:19 Edwin BermejoEdwin Bermejo 4423 gold badges5 silver badges17 bronze badges 3
  • Where's your code you used to generate the former object in the question? – Mohammad Yusuf Commented Nov 30, 2016 at 8:22
  • check your output data, its not validate – stasovlas Commented Nov 30, 2016 at 8:29
  • I have this data from may db: Generic Drugs | Dulcolax | Bisacodyl ---------------------------------------------- Generic Drugs | Vitamin C | Ascorbic Acid ----------------------------------------------------- Generic Drugs | Celine | Ascorbic Acid ------------------------------------------------------- Non-generic Drugs | test 2 | ------------------------------------------------------- Non-generic Drugs | Alaxan FR | used this code to e up with the first object _.groupBy(response.rs ,'revDescription'); – Edwin Bermejo Commented Nov 30, 2016 at 8:33
Add a ment  | 

1 Answer 1

Reset to default 3

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./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;
});
发布评论

评论列表(0)

  1. 暂无评论