Working on a JavaScript project where I need to group JSON object values dynamically based on a specific key word in the array.
Each object in the array contains time values.
The length of the array, called time_entries, can vary.
This array can have one or multiple objects in it.
The goal is to group the objects based on the time: Hours, minutes, seconds.
{
"activities": [
{
"name": "player one",
"time_entries": [
{
"days": 0,
"end_time": "2019-09-30 15:19:43",
"hours": 01,
"minutes": 02,
"seconds": 11,
"start_time": "2019-09-30 14:17:58"
},
{
"days": 0,
"end_time": "2019-09-25 15:40:11",
"hours": 0,
"minutes": 20,
"seconds": 4,
"start_time": "2019-09-25 15:20:15"
},
{
"days": 0,
"end_time": "2019-09-25 16:10:15",
"hours": 0,
"minutes": 30,
"seconds": 4,
"start_time": "2019-09-25 15:40:11"
},
#more objects can be added here
]
},
{
"name": "player two",
"time_entries": [
{
"days": 0,
"end_time": "2019-09-30 19:18:51",
"hours": 0,
"minutes": 0,
"seconds": 52,
"start_time": "2019-09-30 19:17:58"
},
#more objects can be added here
]
},
{
"name": "player three",
"time_entries": [
{
"days": 0,
"end_time": "2019-09-30 19:19:09",
"hours": 0,
"minutes": 0,
"seconds": 58,
"start_time": "2019-09-30 19:18:51"
},
{
"days": 0,
"end_time": "2019-09-30 21:21:09",
"hours": 2,
"minutes": 1,
"seconds": 0,
"start_time": "2019-09-30 19:20:09"
},
#more objects can be added here
]
}
]
}
The desired output should look like this:
[
{
"name": "player one",
"time_entries": [
{
"days": 0,
"end_time": "2019-09-25 16:10:15",
"hours": 01,
"minutes": 50,
"seconds": 19,
"start_time": "2019-09-30 14:17:58"
}
]
},
{
"name": "player two",
"time_entries": [
{
"days": 0,
"end_time": "2019-09-30 19:18:51",
"hours": 0,
"minutes": 0,
"seconds": 52,
"start_time": "2019-09-30 19:17:58"
}
]
},
{
"name": "player three",
"time_entries": [
{
"days": 0,
"end_time":"2019-09-30 21:21:09",
"hours": 2,
"minutes": 1,
"seconds": 58,
"start_time": "2019-09-30 19:18:51"
}
]
}
]