I am currently struggling with how to solve nested grouping of data.
The initial structure is given and my grouped structure below as well as my approach can be adjusted.
My idea was to have the array of objects grouped by multiple parameters in a nested way. Let me show an example structure:
- City: Zurich
-- Date: 2024/09/12
--- All entries of that city/date
-- Date yyyy/mm/dd
--- More entries
- City: Berlin
-- Date: 2024/09/12
--- All entries of that city/date
If there is other ways to solve this instead of groupBy, please help me understand how to approach it.
The data
My initial array looks like this (fetched as JSON data):
[
{
"_id": "123",
"title": "Musician 1",
"date": "2024-09-12T00:00:00.000",
"city": "zurich",
"country": "ch"
},
{
"_id": "124",
"title": "Musician 2",
"date": "2024-09-14T00:00:00.000",
"city": "berlin",
"country": "de"
},
{
"_id": "125",
"title": "Musician 3",
"date": "2024-09-14T00:00:00.000",
"city": "berlin",
"country": "de"
},
{
"_id": "126",
"title": "Musician 4",
"date": "2024-10-24T00:00:00.000",
"city": "zurich",
"country": "ch"
}
]
My approach (groups)
My idea is to add an object that has the information on what key is grouped and what value. Something like this:
[
{
"groupBy": "country",
"value": "ch",
"data": [
{
"_id": "123",
"title": "Musician 1",
"date": "2024-09-12T00:00:00.000",
"city": "zurich",
"country": "ch"
},
{
"_id": "126",
"title": "Musician 4",
"date": "2024-10-24T00:00:00.000",
"city": "zurich",
"country": "ch"
}
]
},
{
"groupBy": "country",
"value": "de",
"data": [
{
"_id": "124",
"title": "Musician 2",
"date": "2024-09-14T00:00:00.000",
"city": "berlin",
"country": "de"
},
{
"_id": "125",
"title": "Musician 3",
"date": "2024-09-14T00:00:00.000",
"city": "berlin",
"country": "de"
}
]
}
]
And the nested groupBy would look like this:
[
{
"groupBy": "country",
"value": "ch",
"data": [
{
"groupBy": "date",
"value": "2024-09-12",
"data": [
{
"_id": "123",
"title": "Musician 1",
"date": "2024-09-12T00:00:00.000",
"city": "zurich",
"country": "ch"
}
]
},
{
"groupBy": "date",
"value": "2024-10-24",
"data": [
{
"_id": "126",
"title": "Musician 4",
"date": "2024-10-24T00:00:00.000",
"city": "zurich",
"country": "ch"
}
]
}
]
},
{
"groupBy": "country",
"value": "de",
"data": [
{
"groupBy": "date",
"value": "2024-09-14",
"data": [
{
"_id": "124",
"title": "Musician 2",
"date": "2024-09-14T00:00:00.000",
"city": "berlin",
"country": "de"
},
{
"_id": "125",
"title": "Musician 3",
"date": "2024-09-14T00:00:00.000",
"city": "berlin",
"country": "de"
}
]
}
]
}
]
This looks kinda repetitive, and I am not sure how simple that will be to code.
My idea is to build this structure with this code workflow (in JavaScript):
- Call
groupByNested(data, ['country', 'date'])
- Creates a new array with the unique data of 'country'
- Loop through the data array and save every object that equals the parameter1 (country) to its new structure
- The data Array is now 2-dimensional
- When finished, loop through the new Array
- Recursively call
groupByNested(GroupedArray1, ['date'])
but remove the parameter that was already done (country) - If parameter array is empty return the array
- Recursively call
As soon as I have the grouped array, I can just create nested loops to actually use it.
This sounds all overly complicated to me and I would be happy if you could provide me with easier ideas/solutions for this problem.