-1

Here is my response after some operations. Based on empid I want to group districts, products and get the count of sms and whatsapp.

[
  {
    "_id": {
      "empid": "sindhu",
      "district": "Hyderabad",
      "product": "Fair Fertilizers"
    },
    "sms": 1
  },
  {
    "_id": {
      "empid": "nagaraju",
      "district": "Guntur",
      "product": "Fair Fertilizers"
    },
    "sms": 2
  },
  {
    "_id": {
      "empid": "sindhu",
      "district": "Hyderabad",
      "product": "Fair Fertilizers"
    },
    "whatsapp": 2
  },
  {
    "_id": {
      "empid": "sindhu",
      "district": "Krishna",
      "product": "Fair Fertilizers"
    },
   "whatsapp": 2
  }
]

I want the above data in the format as shown below.

[
  {
    "sindhu":[
        {
          "district": "Hyderabad",
          "product": "Fair Fertilizers",
          "sms": 1,
          "whatsapp": 2
        },
        {
          "district": "Krishna",
          "product": "Fair Fertilizers",
          "whatsapp": 2
        },
    ]
  },
  {
    "nagaraju" : [
        {
          "district": "Guntur",
          "product": "Fair Fertilizers",
          "sms": 2
        }
     ]
  }
]

In order to get like that I tried the below code. But I didn't get it.

var groups = Object.create(null);
for (var i = 0; i < allTools.length; i++) {
    var item = allTools[i];
    if(!groups[item._id]) {
        groups[item._id] = [];
    }
    groups[item._id].push({
        district: item.district,
        product: item.product,
        sms: item.sms,
        whatsapp: item.whatsapp,
        mailing: item.mailing,
        telecalling: item.telecalling,
        enquiry: item.enquiry
    });
}
var data = [];
for (var x in groups) {
    var obj = {};
    obj[x] = groups[x];
    data.push(obj);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
G.Mounika
  • 385
  • 1
  • 3
  • 16
  • 4
    Possible duplicate of [What is the most efficient method to groupby on a javascript array of objects?](https://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) – Rajesh May 22 '17 at 14:06
  • 1
    please share some thoughts and what you have tried. – Nina Scholz May 22 '17 at 14:08
  • @NinaScholz, I have edited the question. Please look into it. – G.Mounika May 23 '17 at 05:33

1 Answers1

1

You could use an iterative an nested approach for building the necessary references to the result array by using a hash table which works for using keys as well as for keeping part results.

A greater simplifying is not possible, because of the different need of every level of the grouping keys of empid and district.

var data = [{ _id: { empid: "sindhu", district: "Hyderabad", product: "Fair Fertilizers" }, sms: 1 }, { _id: { empid: "nagaraju", district: "Guntur", product: "Fair Fertilizers" }, sms: 2 }, { _id: { empid: "sindhu", district: "Hyderabad", product: "Fair Fertilizers" }, whatsapp: 2 }, { _id: { empid: "sindhu", district: "Krishna", product: "Fair Fertilizers" }, whatsapp: 2 }],
    values = ['sms', 'whatsapp'],
    result = [],
    hash = { _: result };

data.forEach(function (o) {
    var temp = {},
        reference = hash;

    if (!reference[o._id.empid]) {
        reference[o._id.empid] = { _: [] };
        temp[o._id.empid] = reference[o._id.empid]._;
        reference._.push(temp);
    }
    reference = reference[o._id.empid];

    if (!reference[o._id.district]) {
        reference[o._id.district] = { district: o._id.district, product: o._id.product };
        reference._.push(reference[o._id.district]);
    }
    reference = reference[o._id.district];

    values.forEach(function (k) {
        if (k in o) {
            reference[k] = o[k];
        }
    });
});

console.log(result);
console.log(hash);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392