简体   繁体   中英

Groupby multiple keys with lodash or plain js

I've been trying to group by multiple keys with lodash but it's not returning the correct results. The aim is to group by state and details but in this case it's joining/grouping docIds 333333 and 444444 which although have equal state (rejected), don't have the same id 's in details (=> different people) so they don't share both conditions: state and id 's in details .

It seems my current code does work with multiple criteria but once only one of the conditions exists it still performs the grouping, while I wanted to only group if both criteria were satisfied.

As for lodash approach It should show:

[
  {
    "docId": "222222,1111111",
    "details": [
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      },
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Fellow Last Name"
      }
    ],
    "state": "accepted",
  },
  {
    "docId": "333333",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Fellow Last Name"
      }
    ],
    "state": "rejected",
  },
  {
    "docId": "444444",
    "details": [
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      },
    ],
    "state": "rejected",
  }
]

I'm open to use plain js and have no problem in getting for example "docId": [{"333333"},{"444444"}] instead of comma separated values, or slightly different final result as long as the grouping in the end obeys to the same rules but I wasn't able to achieve the intended result either with plain js so I moved to lodash which seemed simpler.

In the end I will be needing some sorting to prioritize state , then groups with only one person in details and when one person, prioritize the ones with claimant but that's something that should be done afterwards, right?

Help would be much appreciated.

 const data = [ { docId: 222222, state: "accepted", details: [ { id: 20656, type: "Claimant", name: "First Name Last Name", }, { id: 10000, type: "Fellow", name: "Fellow First Name Fellow Last Name", } ] }, { docId: 1111111, state: "accepted", details: [ { id: 10000, type: "Fellow", name: "Fellow First Name Fellow Last Name", }, { id: 20656, type: "Claimant", name: "First Name Last Name", } ] }, { docId: 333333, state: "rejected", details: [ { id: 10000, type: "Fellow", name: "Fellow First Name Last Name", } ] }, { docId: 444444, state: "rejected", details: [ { id: 20656, type: "Claimant", name: "First Name Last Name", } ] } ]; const grouped = _(data).groupBy(({details,state}) => `${details},${state}`).map((value, key) => ({ docId: _.map(value, 'docId').join(','), details: value[0].details, state: value[0].state, document_file_name: value[0].document_file_name, })).value() console.log(grouped)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

First collect id by state and docId . Second group collected data by state and sorted id and build new objects.

 const data = [{ docId: 222222, document_file_name: "4020653_FileName.pdf", document_updated_at: "2020-07-08T19:41:28.385Z", state: "accepted", details: [{ id: 20656, type: "Claimant", name: "First Name Last Name" }, { id: 10000, type: "Fellow", name: "Fellow First Name Fellow Last Name" }] }, { docId: 1111111, document_file_name: "4020600_FileName.pdf", document_updated_at: "2020-07-08T19:41:28.385Z", state: "accepted", details: [{ id: 10000, type: "Fellow", name: "Fellow First Name Fellow Last Name" }, { id: 20656, type: "Claimant", name: "First Name Last Name" }] }, { docId: 333333, document_file_name: "4020890_FileName.pdf", document_updated_at: "2020-07-08T19:41:28.385Z", state: "rejected", details: [{ id: 10000, type: "Fellow", name: "Fellow First Name Last Name" }] }, { docId: 444444, document_file_name: "4020672_FileName.pdf", document_updated_at: "2020-07-08T19:41:28.385Z", state: "rejected", details: [{ id: 20656, type: "Claimant", name: "First Name Last Name" }] }], ids = {}, temp = data.reduce((r, { docId, state, details }) => { const key = [state, docId].join('|'); details.forEach(o => { ids[o.id] = o; (r[key]??= []).push(o.id); }); return r; }, {}), grouped = Object.values(Object.entries(temp).reduce((r, [k, v]) => { const [state, docId] = k.split('|'), key = [state, v.sort().join('|')].join('#'); r[key]??= { docId: '', details: v.map(id => ids[id]), state }; r[key].docId += (r[key].docId && ', ') + docId; return r; }, {})); console.log(grouped);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM