Rohit Ambre
Rohit Ambre

Reputation: 961

groupBy array based on combination of 2 properties

I want to Group array elements based on combination of 2 properties.

I have an array containing FirstDate and LastDate, where I want to group objects with same combination of FirstDate and LastDate and create Object with any random unique string as Key OR Array of arrays anything would work.

I have following array,

const data = [ 
    { 
       InvDate: "2021-05-04 00:00:00",
       EntityID: 4309,
       FirstDate: "2021-05-03 18:30:00",
       LastDate: "2021-05-04 18:30:00"
    },
    {  
       InvDate: "2021-05-04 00:00:00",
       EntityID: 4314,
       FirstDate: "2021-05-03 18:30:00",
       LastDate: "2021-05-04 18:30:00"
    },
    {  
       InvDate: "2021-05-04 00:00:00",
       EntityID: 4464,
       FirstDate: "2021-05-01 18:30:00",
       LastDate: "2021-05-04 18:30:00"
    },
    {  
       InvDate: "2021-05-04 00:00:00",
       EntityID: 4465,
       FirstDate: "2021-04-20 18:30:00",
       LastDate: "2021-05-04 18:30:00"
    }
]

I want to group object having similar FirstDate and LastDate, so for example result should look like,

// Array of arrays example
const result1 = [ 
    [
        { 
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4309,
            FirstDate: "2021-05-03 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        },
        {  
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4314,
            FirstDate: "2021-05-03 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        },
    ],
    [
        {  
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4464,
            FirstDate: "2021-05-01 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        },
    ],
    [
        {  
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4465,
            FirstDate: "2021-04-20 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        }
    ]
]

OR

// GroupedBy object with any random key example
const result2 = { 
    "random1": [
        { 
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4309,
            FirstDate: "2021-05-03 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        },
        {  
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4314,
            FirstDate: "2021-05-03 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        },
    ],
    "random2": [
        {  
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4464,
            FirstDate: "2021-05-01 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        },
    ],
    "random3": [
        {  
            InvDate: "2021-05-04 00:00:00",
            EntityID: 4465,
            FirstDate: "2021-04-20 18:30:00",
            LastDate: "2021-05-04 18:30:00"
        }
    ]
}

It's in node, doesn't have to be in VaniallaJS, use of Lodash function would work, as I already have it installed in my app.

Upvotes: 1

Views: 50

Answers (1)

georg
georg

Reputation: 215009

With lodash groupBy you can use a callback ("iteratee") to create a compound key to group by two fields at once. In your example this is easy, because all dates have the same format, so you can simply concatenate (add) them to create unique compound keys. That is,

_.groupBy(data, item => item.FirstDate + item.LastDate)

should do what you want.

In the general case, where keys can be arbitrary strings or even other types, you can use JSON to combine them into a single string key, for example:

_.groupBy(data, item => JSON.stringify([item.firstKey, item.secondKey]))

Upvotes: 1

Related Questions