groupBy array based on combination of 2 properties

61
May 06, 2021, at 6:00 PM

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.

Answer 1

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]))
READ ALSO
How to add time stamp to push notifications when on group - Raact Native - Android

How to add time stamp to push notifications when on group - Raact Native - Android

I have implemented push notifications on android and ios

53
The width of the footer of one of my web pages is shrinking more than it should on a small screen and not appearing at the bottom - how to correct it?

The width of the footer of one of my web pages is shrinking more than it should on a small screen and not appearing at the bottom - how to correct it?

The width of the footer of one of my webpage is shrinking more than it should on a small screen and not appearing at the bottom - how to correct it?

90
PHP how do I make a button which will not make an image transparent

PHP how do I make a button which will not make an image transparent

So I am making an image game and I want the images to reappear from being transparent when a button is pressedHow do I do this in php

56