1

I have array in javascript

Now I have to groupby this array on field entityName and appointmentName to make output like this

1
  • And what have you tried already? Commented Jun 27, 2016 at 11:20

3 Answers 3

2

You could use the smart Array#forEach with an object for referencing the group.

var companyArr = [{ entityName: "ABC Company, LLC", appointmentName: "ABCware", eventName: "Annual Report", dueDate: "2017-03-01" }, { entityName: "ABC Company, LLC", appointmentName: "ABCware", eventName: "Business Licence Renewal", dueDate: "2017-06-01" }, { entityName: "XYZ Companies, LLC", appointmentName: "XYZWare", eventName: "Annual Report - II", dueDate: "2016-06-27" }],
    newCompanyArr = [];

companyArr.forEach(function (a) {
    var key = a.entityName + '|' + a.appointmentName;
    if (!this[key]) {
        this[key] = { entityName: a.entityName, appointmentName: a.appointmentName, eventArray: [] };
        newCompanyArr.push(this[key]);
    }
    this[key].eventArray.push({ eventName: a.eventName, dueDate: a.dueDate });
}, Object.create(null));

console.log(newCompanyArr);

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

Comments

0

Made a fiddle for this:

JavaScript

var companyArr = [{
  entityName : "ABC Company, LLC",
  appointmentName : "ABCware",
  eventName: " Annual Report",
  dueDate : " 2017-03-01"
},{
  entityName : "ABC Company, LLC",
  appointmentName : "ABCware",
  eventName: " Business Licence Renewal",
  dueDate : " 2017-06-01"
},{
  entityName : "XYZ Companies, LLC",
  appointmentName: "XYZWare",
  eventName : " Annual Report - II",
  dueDate : " 2016-06-27"
}];

var newCompanyArr = [];
var processedCompanies = [];

for(var i = 0; i < companyArr.length; i++) {
  var company = companyArr[i];
  var key = company.entityName + ' - ' + company.appointmentName;
  if(processedCompanies.indexOf(key) === -1) {
    var results = companyArr.filter(function(comp) {
        return comp.entityName === company.entityName && comp.appointmentName === company.appointmentName;
    });
    var newCompany = {
        entityName: company.entityName,
      appointmentName: company.appointmentName,
      eventArray: []
    }
    for(var y = 0; y < results.length; y++) {
        var res = results[y];
        newCompany.eventArray.push({
        eventName: res.eventName,
        dueDate: res.dueDate
      })
    }
    newCompanyArr.push(newCompany);
    processedCompanies.push(key);
  }
}

console.log(JSON.stringify(newCompanyArr, null, 2));

forEach does not work on all browsers (IE < 9).

Comments

-1

Here is a working plnkr

Commented Code

var companyArr = [{
  entityName : "ABC Company, LLC",
  appointmentName : "ABCware",
  eventName: " Annual Report",
  dueDate : " 2017-03-01"
},{
  entityName : "ABC Company, LLC",
  appointmentName : "ABCware",
  eventName: " Business Licence Renewal",
  dueDate : " 2017-06-01"
},{
  entityName : "XYZ Companies, LLC",
  appointmentName: "XYZWare",
  eventName : " Annual Report - II",
  dueDate : " 2016-06-27"
}];

// so we need to group by a particular key, so create the dictionary
var groupByDict = {};
for (var i = 0; i < companyArr.length; i++) {
  var event = companyArr[i];
  // here is the composite key we're going to group by
  var key = event.entityName + " " + event.appointmentName;

  // we need to get the pre-existing group if there is one
  var grouping = groupByDict[key];

  // we use the || to say if there isn't a value, prepopulate it with one
  grouping = grouping || {
    entityName: event.entityName,
    appointmentName: event.appointmentName,
    eventArray: []
  };

  // then we need to push the event details into the event array for this grouping
  grouping.eventArray.push({
    eventName: event.eventName,
    dueDate: event.dueDate
  });

  // then put the grouping back into the group by dictionary
  groupByDict[key] = grouping;
}

// finally you wanted them as an array of groupings
var groupsAsArray = [];

// so here we just iterate through all of the keys in the dictionary
for (var key in groupByDict) {
  if (!groupByDict.hasOwnProperty(key)) continue;

  // if it is a key we care about then push them into the array
  groupsAsArray.push(groupByDict[key]);
}

console.log(groupsAsArray);

1 Comment

I still don't understand why there is a downvote on a correct answer that supports ECMA 3 and above. Which is clearly demonstrating the logic behind grouping...

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.