I have a small web-application which fetches data using API call, the response is array of reports, each report have unique id, application, type and title.
I want to map it into new object by grouping-by application and then by types as describe bellow.
Input:
[
{
"id": "REPORT1",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT2",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT3",
"application": "APP1",
"type": "TYPE2",
"title": ""
},
{
"id": "REPORT4",
"application": "APP2",
"type": "TYPE3",
"title": ""
}
]
Desired output:
{
"APP1": {
"TYPE1": [
{
"id": "REPORT1",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT2",
"application": "APP1",
"type": "TYPE1",
"title": ""
}
],
"TYPE2": [
{
"id": "REPORT3",
"application": "APP1",
"type": "TYPE2",
"title": ""
}
]
},
"APP2": {
"TYPE3": [
{
"id": "REPORT4",
"application": "APP2",
"type": "TYPE3",
"title": ""
}
]
}
}
With loadash I came up with this:
const output = _(input)
.groupBy(report => report.application)
.value()
After group by application, I need to make another nested grouping or mapping by type but got stuck.