Using Lodash to group nested objects

Viewed 33

I currently have an array with two objects. Both objects have 5 or so top level key/value pairs with one of them called items that has nest objects in them. I'm attempting to do is to have the items to have a key on it called eventNames and within that are the records. I need to keep them sorted by their category as they currently are. I just need the records with the same eventName to be grouped together within items.

Essentially when the user goes to this page. They will see two bootstrap accordions. One for withHeld and one for approvedSent. Then there's another accordion in each with a list of event names, then within each event name are the events that correspond to that event.

It says "Mail Type" this is the category key, and it shows how many records next to it (the recordCount), and within each they should see events, and within each event they'll see info on the actual event (I removed event info in items)

[
    {
        "_showDetails": false,
        "category": "withHeld",
        "venueID": "VA92944B-0",
        "startDate": "2022-10-06T04:00:00.000Z",
        "recordCount": 32,
        "currentPage": 1,
        "items": [
            {
                "mailType": "withHeld",
                "id": "bc09dabd28ec1295cc6d91883f6e8a2f",
                "eventName": "Lifestyle Initiative",
            },
            {
                "mailType": "withHeld",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lincoln NerdCon 2022",
            },
            {
                "mailType": "withHeld",
                "id": "bc09dabd28ec1295cc6d91883f6e8a2f",
                "eventName": "Richardtown Town Centre",
            },
            {
                "mailType": "withHeld",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lifestyle Initiative",
            },
            {
                "mailType": "withHeld",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lincoln Biker Battle 3",
            },
            {
                "mailType": "withHeld",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lincoln NerdCon 2022",
            },
        ],
    },
    {
        "_showDetails": false,
        "category": "approvalSent",
        "venueID": "NB98a887-2",
        "startDate": "2022-12-11T05:00:00.000Z",
        "recordCount": 13,
        "currentPage": 1,
        "items": [
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lifestyle Initiative 2022",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lifestyle Initiative 2022",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lifestyle Initiative 2022",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "RoadCycle Club",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lincoln NerdCon 2022",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Cereal Convention 4",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Hybrid Driver Car Meet",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Yorkshire Owners of Lincoln",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "ClubBMW Meet",
            },
            {
                "mailType": "approvalSent",
                "id": "bcf32u89999890cc909021f",
                "eventName": "Lincoln NerdCon 2022",
            },

        ],
    }
]

EDIT: Forgot to add my lodash approach was so focused on editing the data, apologies!

const groupByEvent = mailDataGroup.map((record) => ({
  eventName: _.groupBy(record.items, "eventName")
}));

This creates an object called "eventNames", so what I need to do now is insert "eventNames" array of objects into items array for each category.

To give an overview of what the frontend should look like

MAIL TYPE

  • Withheld
  • Approval Sent
    • Lifestyle Initiative 2022
      • record 1
      • record 2
      • record 3
    • ClubBMW Meet
    • Lincoln NerdCon 2022

Hopefully I made that clear enough? I've been sitting here spinning my wheels on this for a day trying to figure this out via lodash, any direction would be much appreciated.

0 Answers
Related