I have an array of objects like below
[
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-07"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-08"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-09"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-10"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-11"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-07"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-08"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-09"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-10"
]
}
},
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-11"
]
}
},
{
"medication": {
"name": "Septron",
"time": [
"2018-09-07"
]
}
},
{
"medication": {
"name": "Septron",
"time": [
"2018-09-08"
]
}
},
{
"medication": {
"name": "Septron",
"time": [
"2018-09-09"
]
}
},
{
"medication": {
"name": "Septron",
"time": [
"2018-09-10"
]
}
},
{
"medication": {
"name": "Septron",
"time": [
"2018-09-11"
]
}
}
]
lodash or underscore provided great functions to minimalise the code, but still those libraries didn't help me solve the above problem
I want to achieve something like below
[
{
"medication": {
"name": "Turbohaler",
"time": [
"2018-09-07",
"2018-09-08",
"2018-09-09",
"2018-09-10",
"2018-09-11"
]
}
},
{
"medication": {
"name": "Septron",
"time": [
"2018-09-07",
"2018-09-08",
"2018-09-09",
"2018-09-10",
"2018-09-11"
]
}
} ]
The above result is formed by grouping an array of objects with same key name (Medication) in our case, and fetching all its values together and merging it into a single object in an array. I have tried different approaches and it failed, If someone could try a bit for above expected output, it will be much appreciated
Thanks in advance