由子元素组成的lodash组

时间:2017-06-15 20:05:01

标签: underscore.js lodash

我的数据形式如下:

[{
  "OpportunityName": "DSW - Addition",
  "EstStartDate": "2017-07-10T04:00:00Z",
  "Duration": 24,
  "WalkThroughDate": "2017-06-12T04:00:00Z",
  "DueDate": "2017-05-12T04:00:00Z",
  "Description": null,
  "ProjectManager": "Rick Deloreto",
  "FileDownloadURL": null,
  "Inv": [{
    "BidListID": "76c7efbb-a32a-e711-80bd-00155d51d601",
    "LastInviteType": "Initial",
    "EstimatingContact": null,
    "Trade": "16500",
    "SentDate": "2017-04-26T17:34:22Z",
    "Note": null,
    "ResponseDate": "2017-05-04T14:12:29Z",
    "ResponseStatus": "Yes",
    "ProjectFileAccessDate": "2017-03-03T18:48:30Z",
    "fileDateAdded": "2017-06-15T14:40:42Z",
    "fileURL": "https://www.dropbox.com/s/3qut4lstudr8h8x/logo%20%281%29.txt?dl=1"
  }]
}, {
  "OpportunityName": "Old Navy",
  "EstStartDate": "2017-09-01T04:00:00Z",
  "Duration": 43,
  "WalkThroughDate": "2017-06-01T04:00:00Z",
  "DueDate": "2017-05-01T04:00:00Z",
  "Description": "this is the description for the opportunity",
  "ProjectManager": "Matt Chinnici",
  "FileDownloadURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1",
  "Inv": [{
    "BidListID": "3c85be51-860d-e711-80bc-00155d51d601",
    "LastInviteType": "Second Request",
    "EstimatingContact": null,
    "Trade": "16200",
    "SentDate": "2017-04-10T20:00:00Z",
    "Note": null,
    "ResponseDate": "2017-05-04T17:50:37Z",
    "ResponseStatus": "Yes",
    "ProjectFileAccessDate": "2017-02-02T15:00:00Z",
    "fileDateAdded": "2017-05-01T14:10:08Z",
    "fileURL": "https://www.dropbox.com/s/0v70dcsa8lz0ncf/fueldistribution.jpg?dl=1"
  }, {
    "BidListID": "5a9a72d3-181b-e711-80bc-00155d51d601",
    "LastInviteType": "Initial",
    "EstimatingContact": null,
    "Trade": "16100",
    "SentDate": "2017-04-12T20:00:00Z",
    "Note": null,
    "ResponseDate": "2017-05-04T17:50:48Z",
    "ResponseStatus": "Yes",
    "ProjectFileAccessDate": "2017-03-03T15:27:17Z",
    "fileDateAdded": "0001-01-01T00:00:00",
    "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
  }, {
    "BidListID": "a3fe94da-181b-e711-80bc-00155d51d601",
    "LastInviteType": "Initial",
    "EstimatingContact": null,
    "Trade": "16500",
    "SentDate": "2017-03-18T00:00:00Z",
    "Note": null,
    "ResponseDate": "2017-04-01T14:12:56Z",
    "ResponseStatus": "No",
    "ProjectFileAccessDate": "2017-03-03T17:33:40Z",
    "fileDateAdded": "0001-01-01T00:00:00",
    "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
  }]
}, {
  "OpportunityName": "Trader Joe",
  "EstStartDate": "2017-04-26T04:00:00Z",
  "Duration": 0,
  "WalkThroughDate": "0001-01-01T00:00:00",
  "DueDate": "2017-04-04T04:00:00Z",
  "Description": "Project Description\n",
  "ProjectManager": null,
  "FileDownloadURL": null,
  "Inv": [{
    "BidListID": "afda45b7-191b-e711-80bc-00155d51d601",
    "LastInviteType": "Addendum",
    "EstimatingContact": null,
    "Trade": "16100",
    "SentDate": "2017-04-26T00:55:30Z",
    "Note": null,
    "ResponseDate": "2017-04-01T14:13:09Z",
    "ResponseStatus": "Maybe",
    "ProjectFileAccessDate": "0001-01-01T00:00:00",
    "fileDateAdded": "2017-04-24T16:17:56Z",
    "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
  }]
}];

并希望按照ResponseStatus属性对每个对象进行分组,以便该对象类似于以下内容:

{
  "New": [],
  "Yes": [
    {
      "OpportunityName": "DSW - Addition",
      "EstStartDate": "2017-07-10T04:00:00Z",
      "Duration": 24,
      "WalkThroughDate": "2017-06-12T04:00:00Z",
      "DueDate": "2017-05-12T04:00:00Z",
      "Description": null,
      "ProjectManager": "Rick Deloreto",
      "FileDownloadURL": null,
      "Inv": [
        {
          "BidListID": "76c7efbb-a32a-e711-80bd-00155d51d601",
          "LastInviteType": "Initial",
          "EstimatingContact": null,
          "Trade": "16500",
          "SentDate": "2017-04-26T17:34:22Z",
          "Note": null,
          "ResponseDate": "2017-05-04T14:12:29Z",
          "ResponseStatus": "Yes",
          "ProjectFileAccessDate": "2017-03-03T18:48:30Z",
          "fileDateAdded": "2017-06-15T14:40:42Z",
          "fileURL": "https://www.dropbox.com/s/3qut4lstudr8h8x/logo%20%281%29.txt?dl=1"
        }
      ]
    },
    {
      "OpportunityName": "Old Navy",
      "EstStartDate": "2017-09-01T04:00:00Z",
      "Duration": 43,
      "WalkThroughDate": "2017-06-01T04:00:00Z",
      "DueDate": "2017-05-01T04:00:00Z",
      "Description": "this is the description for the opportunity",
      "ProjectManager": "Matt Chinnici",
      "FileDownloadURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1",
      "Inv": [
        {
          "BidListID": "5a9a72d3-181b-e711-80bc-00155d51d601",
          "LastInviteType": "Initial",
          "EstimatingContact": null,
          "Trade": "16100",
          "SentDate": "2017-04-12T20:00:00Z",
          "Note": null,
          "ResponseDate": "2017-05-04T17:50:48Z",
          "ResponseStatus": "Yes",
          "ProjectFileAccessDate": "2017-03-03T15:27:17Z",
          "fileDateAdded": "0001-01-01T00:00:00",
          "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
        }
      ]
    }
  ],
  "No": [
    {
      "OpportunityName": "Old Navy",
      "EstStartDate": "2017-09-01T04:00:00Z",
      "Duration": 43,
      "WalkThroughDate": "2017-06-01T04:00:00Z",
      "DueDate": "2017-05-01T04:00:00Z",
      "Description": "this is the description for the opportunity",
      "ProjectManager": "Matt Chinnici",
      "FileDownloadURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1",
      "Inv": [
        {
          "BidListID": "3c85be51-860d-e711-80bc-00155d51d601",
          "LastInviteType": "Second Request",
          "EstimatingContact": null,
          "Trade": "16200",
          "SentDate": "2017-04-10T20:00:00Z",
          "Note": null,
          "ResponseDate": "2017-05-04T20:01:13Z",
          "ResponseStatus": "No",
          "ProjectFileAccessDate": "2017-02-02T15:00:00Z",
          "fileDateAdded": "2017-05-01T14:10:08Z",
          "fileURL": "https://www.dropbox.com/s/0v70dcsa8lz0ncf/fueldistribution.jpg?dl=1"
        },
        {
          "BidListID": "a3fe94da-181b-e711-80bc-00155d51d601",
          "LastInviteType": "Initial",
          "EstimatingContact": null,
          "Trade": "16500",
          "SentDate": "2017-03-18T00:00:00Z",
          "Note": null,
          "ResponseDate": "2017-04-01T14:12:56Z",
          "ResponseStatus": "No",
          "ProjectFileAccessDate": "2017-03-03T17:33:40Z",
          "fileDateAdded": "0001-01-01T00:00:00",
          "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
        }
      ]
    }
  ],
  "Maybe": [
    {
      "OpportunityName": "Trader Joe",
      "EstStartDate": "2017-04-26T04:00:00Z",
      "Duration": 0,
      "WalkThroughDate": "0001-01-01T00:00:00",
      "DueDate": "2017-04-04T04:00:00Z",
      "Description": "Project Description\n",
      "ProjectManager": null,
      "FileDownloadURL": null,
      "Inv": [
        {
          "BidListID": "afda45b7-191b-e711-80bc-00155d51d601",
          "LastInviteType": "Addendum",
          "EstimatingContact": null,
          "Trade": "16100",
          "SentDate": "2017-04-26T00:55:30Z",
          "Note": null,
          "ResponseDate": "2017-04-01T14:13:09Z",
          "ResponseStatus": "Maybe",
          "ProjectFileAccessDate": "0001-01-01T00:00:00",
          "fileDateAdded": "2017-04-24T16:17:56Z",
          "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
        }
      ]
    }
  ]
}

我已经尝试过手动迭代数组并将内容推送到新数组,但它已经非常草率了。寻找有关如何使用lodash实现这一目标的建议。

2 个答案:

答案 0 :(得分:0)

您可以lodash#reducelodash#groupBy一起使用来实现一组传播项目。

// The reduce function transforms the entire collection
// into a group of opportunities relative to each of 
// the Inv item's ResponseStatus
var result = _.reduce(data, function(result, item) {
  // chain the Inv array to be a candidate for grouping
  return _(item.Inv)
    // group each item of Inv by the ResponseStatus
    .groupBy('ResponseStatus')
    // Use reduce to concatenate each Inv item group to
    // to their respective groups
    .reduce(function(rs, group, key) {

      // transform the grouped Invs into it's own opportunity
      var opportunity = _.assign({}, item, { Inv: group });

      // concatenate the opportunity in accordance to it's group
      rs[key] = rs[key].concat(opportunity);

      // return the result
      return rs;
    }, result);
}, { New: [], Yes: [], No: [], Maybe: [] }); // default result

var data = [{
  "OpportunityName": "DSW - Addition",
  "EstStartDate": "2017-07-10T04:00:00Z",
  "Duration": 24,
  "WalkThroughDate": "2017-06-12T04:00:00Z",
  "DueDate": "2017-05-12T04:00:00Z",
  "Description": null,
  "ProjectManager": "Rick Deloreto",
  "FileDownloadURL": null,
  "Inv": [{
    "BidListID": "76c7efbb-a32a-e711-80bd-00155d51d601",
    "LastInviteType": "Initial",
    "EstimatingContact": null,
    "Trade": "16500",
    "SentDate": "2017-04-26T17:34:22Z",
    "Note": null,
    "ResponseDate": "2017-05-04T14:12:29Z",
    "ResponseStatus": "Yes",
    "ProjectFileAccessDate": "2017-03-03T18:48:30Z",
    "fileDateAdded": "2017-06-15T14:40:42Z",
    "fileURL": "https://www.dropbox.com/s/3qut4lstudr8h8x/logo%20%281%29.txt?dl=1"
  }]
}, {
  "OpportunityName": "Old Navy",
  "EstStartDate": "2017-09-01T04:00:00Z",
  "Duration": 43,
  "WalkThroughDate": "2017-06-01T04:00:00Z",
  "DueDate": "2017-05-01T04:00:00Z",
  "Description": "this is the description for the opportunity",
  "ProjectManager": "Matt Chinnici",
  "FileDownloadURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1",
  "Inv": [{
    "BidListID": "3c85be51-860d-e711-80bc-00155d51d601",
    "LastInviteType": "Second Request",
    "EstimatingContact": null,
    "Trade": "16200",
    "SentDate": "2017-04-10T20:00:00Z",
    "Note": null,
    "ResponseDate": "2017-05-04T17:50:37Z",
    "ResponseStatus": "Yes",
    "ProjectFileAccessDate": "2017-02-02T15:00:00Z",
    "fileDateAdded": "2017-05-01T14:10:08Z",
    "fileURL": "https://www.dropbox.com/s/0v70dcsa8lz0ncf/fueldistribution.jpg?dl=1"
  }, {
    "BidListID": "5a9a72d3-181b-e711-80bc-00155d51d601",
    "LastInviteType": "Initial",
    "EstimatingContact": null,
    "Trade": "16100",
    "SentDate": "2017-04-12T20:00:00Z",
    "Note": null,
    "ResponseDate": "2017-05-04T17:50:48Z",
    "ResponseStatus": "Yes",
    "ProjectFileAccessDate": "2017-03-03T15:27:17Z",
    "fileDateAdded": "0001-01-01T00:00:00",
    "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
  }, {
    "BidListID": "a3fe94da-181b-e711-80bc-00155d51d601",
    "LastInviteType": "Initial",
    "EstimatingContact": null,
    "Trade": "16500",
    "SentDate": "2017-03-18T00:00:00Z",
    "Note": null,
    "ResponseDate": "2017-04-01T14:12:56Z",
    "ResponseStatus": "No",
    "ProjectFileAccessDate": "2017-03-03T17:33:40Z",
    "fileDateAdded": "0001-01-01T00:00:00",
    "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
  }]
}, {
  "OpportunityName": "Trader Joe",
  "EstStartDate": "2017-04-26T04:00:00Z",
  "Duration": 0,
  "WalkThroughDate": "0001-01-01T00:00:00",
  "DueDate": "2017-04-04T04:00:00Z",
  "Description": "Project Description\n",
  "ProjectManager": null,
  "FileDownloadURL": null,
  "Inv": [{
    "BidListID": "afda45b7-191b-e711-80bc-00155d51d601",
    "LastInviteType": "Addendum",
    "EstimatingContact": null,
    "Trade": "16100",
    "SentDate": "2017-04-26T00:55:30Z",
    "Note": null,
    "ResponseDate": "2017-04-01T14:13:09Z",
    "ResponseStatus": "Maybe",
    "ProjectFileAccessDate": "0001-01-01T00:00:00",
    "fileDateAdded": "2017-04-24T16:17:56Z",
    "fileURL": "https://www.dropbox.com/s/jmwwv2p5klje82x/Calbrite_logo.JPG?dl=1"
  }]
}];

// The reduce function transforms the entire collection
// into a group of opportunities relative to each of 
// the Inv item's ResponseStatus
var result = _.reduce(data, function(result, item) {
  // chain the Inv array to be a candidate for grouping
  return _(item.Inv)
    // group each item of Inv by the ResponseStatus
    .groupBy('ResponseStatus')
    // Use reduce to concatenate each Inv item group to
    // to their respective groups
    .reduce(function(rs, group, key) {
    
      // transform the grouped Invs into it's own opportunity
      var opportunity = _.assign({}, item, { Inv: group });
      
      // concatenate the opportunity in accordance to it's group
      rs[key] = rs[key].concat(opportunity);
      
      // return the result
      return rs;
    }, result);
}, { New: [], Yes: [], No: [], Maybe: [] }); // default result

console.log(result);
body > div {min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

答案 1 :(得分:0)

这个想法是分裂和重组。

// Splits the data based on "ResponseStatus".
function splitInv(data) {
    return _(data.Inv)
        .groupBy("ResponseStatus")
        .map(function(inv) {
            return _.assign({}, data, { Inv: inv });
        })
        .value();
}

// Splits different "ResponseStatus".
var flatData = _.flatMap(data, splitInv);

// Group by "ResponseStatus".
var result = _.groupBy(flatData, "Inv['0'].ResponseStatus");