I would like to format:
[
{
"id": 1,
"location": {
"id": 1,
"title": "location 1"
},
},
{
"id": 2,
"location": {
"id": 1,
"title": "location 1"
},
},
{
"id": 3,
"location": {
"id": 2,
"title": "location 2"
},
}
]
To:
[
{
"id": 1,
"title": "location 1",
"items": [
{
"id": 1
},
{
"id": 2
}
]
},
{
"id": 2,
"title": "location 2",
"items": [
{
"id": 3
}
]
}
]
Update: From your responses I derived:
const result = _(items)
.groupBy(x => x.location.id)
.map((value, key) => ({location: key, items: value}))
.value();
Which works, but the item objects still contain the location attribute and the location only maintains its id attribute. Is it neccessary to add all attributes to the groupBy?
The reason I am doing this is because I would like to filter on item attributes, but display the items grouped by location.