Group similar object values into new array


#1

I have data like this :

[{
    "ru":"R401","area":"RFCC","unit":"OFFSITE","tot":1
    }, {
    "ru":"R401","area":"RFCC","unit":"OFFSITE","tot":1
    }, {
    "ru":"R401","area":"RFCC","unit":"OFFSITE","tot":1
    }, {
    "ru":"R401","area":"RFCC","unit":"RCU","tot":1
}]

I want to group the objects ‘ru’, ‘area’, ‘unit’, ‘tot’ and using reduce method on ‘tot’ then push it into a new array, but my logic is bad.

And I want the output to be like this :

[{
    "ru":"R401",
    "area":[{
        "RFCC":[{
            "OFFSITE":[{
                "tot":3
            }]
                }, {
            "RCU":[{
                "tot":1
            }]
        }]
    }]
}]

How can I do this? Thanks in advance.