foreach数组用嵌套数组创建新数组

时间:2016-06-27 11:13:47

标签: javascript jquery arrays

我在javascript中有数组

现在我必须将这个数组分组到字段entityNameappointmentName上,以便像这样输出

3 个答案:

答案 0 :(得分:2)

您可以将智能Array#forEach与对象一起用于引用该组。

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);

答案 1 :(得分:0)

为此做了fiddle

的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不适用于所有浏览器(IE&lt; 9)。

答案 2 :(得分:-1)

这是一个有效的plnkr

注释代码

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);