I have a list of dates:
const dates = [
'2022-04-10T00:00:00.000Z',
'2022-04-11T00:00:00.000Z',
'2022-04-12T00:00:00.000Z',
'2022-04-13T00:00:00.000Z',
'2022-04-14T00:00:00.000Z',
'2022-04-15T00:00:00.000Z',
'2022-04-16T00:00:00.000Z',
'2022-04-17T00:00:00.000Z'
]
and I have a list of workouts:
const workouts = [
{
"id": "221bf3b1-2728-41f7-a10e-d004fdd1d0b6",
"title": "ONE MORE TIME",
"description": "Complete for time:\n\n3 rounds of:\n\n•40 Handstand Push-up\n•20 Push press\n\n♂ 135lb ♀ 95lb",
"scheduledAt": "2022-04-10T00:00:00.000Z",
},
{
"id": "3323d0f6-50c9-48cd-9112-540580de0a33",
"title": "DANGER!",
"description": "3 Sets\nAMRAP 8 minutes of:\n\n•20 DB Thrusters\n•10 Pull-Ups\n•30 DB Squats\n\nRest 3 minutes between Amrap \n\n♂ 65 ♀ 45lb",
"scheduledAt": "2022-04-11T00:00:00.000Z",
},
{
"id": "3af2e7d6-6efd-42ad-a09c-72e0022d6ce7",
"title": "Fran",
"description": "Complete for time:\n\n21 - 15 - 9\n\n•Thruster\n•Pull-ups\n\n♂ 95lb ♀ 65lb",
"scheduledAt": "2022-04-12T00:00:00.000Z",
},
{
"id": "4945e72b-0fe5-4779-8f3d-9fdcd53fd406",
"title": "Kabod Tuesday",
"description": "AMRAP 20minutes of:\n\n•30 Hang Squat Clean\n•30 Toes-to-Bar\n•20 Thruster\n•20 Chest-to-Bar\n•10 Cluster\n•10 Muscle Up\n\n♂ 135 ♀ 95lb",
"scheduledAt": "2022-04-13T00:00:00.000Z",
},
{
"id": "5c89574f-ea98-40ef-8e5f-ae38dfbad213",
"title": "Squat Clean 5 sets",
"description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps",
"scheduledAt": "2022-04-13T00:00:00.000Z",
},
{
"id": "7b36c232-aac3-4c96-bc15-d8d523519944",
"title": "Squat Clean 6 sets",
"description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps\n#6: 1 reps",
"scheduledAt": "2022-04-14T00:00:00.000Z",
}
]
I need to check if a date is equal to scheduledAt and add the workout to the new list
In some dates I may not have workouts, and in some dates I may have more than one.
I need to merge both list in something like this:
schedule = [
{
'date': '2022-04-10T00:00:00.000Z',
'workouts': [
{
"id": "221bf3b1-2728-41f7-a10e-d004fdd1d0b6",
"title": "ONE MORE TIME",
"description": "Complete for time:\n\n3 rounds of:\n\n•40 Handstand Push-up\n•20 Push press\n\n♂ 135lb ♀ 95lb",
"scheduledAt": "2022-04-10T00:00:00.000Z",
}
]
},
{
'date': '2022-04-11T00:00:00.000Z',
'workouts': [
{
"id": "3323d0f6-50c9-48cd-9112-540580de0a33",
"title": "DANGER!",
"description": "3 Sets\nAMRAP 8 minutes of:\n\n•20 DB Thrusters\n•10 Pull-Ups\n•30 DB Squats\n\nRest 3 minutes between Amrap \n\n♂ 65 ♀ 45lb",
"scheduledAt": "2022-04-11T00:00:00.000Z",
}
]
},
{
'date': '2022-04-12T00:00:00.000Z',
'workouts': [
{
"id": "3af2e7d6-6efd-42ad-a09c-72e0022d6ce7",
"title": "Fran",
"description": "Complete for time:\n\n21 - 15 - 9\n\n•Thruster\n•Pull-ups\n\n♂ 95lb ♀ 65lb",
"scheduledAt": "2022-04-12T00:00:00.000Z",
}
]
},
{
'date': '2022-04-13T00:00:00.000Z',
'workouts': [
{
"id": "4945e72b-0fe5-4779-8f3d-9fdcd53fd406",
"title": "Kabod Tuesday",
"description": "AMRAP 20minutes of:\n\n•30 Hang Squat Clean\n•30 Toes-to-Bar\n•20 Thruster\n•20 Chest-to-Bar\n•10 Cluster\n•10 Muscle Up\n\n♂ 135 ♀ 95lb",
"scheduledAt": "2022-04-13T00:00:00.000Z",
},
{
"id": "5c89574f-ea98-40ef-8e5f-ae38dfbad213",
"title": "Squat Clean 5 sets",
"description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps",
"scheduledAt": "2022-04-13T00:00:00.000Z",
}
]
},
{
'date': '2022-04-14T00:00:00.000Z',
'workouts': []
},
{
'date': '2022-04-15T00:00:00.000Z',
'workouts': [
{
"id": "7b36c232-aac3-4c96-bc15-d8d523519944",
"title": "Squat Clean 6 sets",
"description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps\n#6: 1 reps",
"scheduledAt": "2022-04-15T00:00:00.000Z",
}
]
},
{
'date': '2022-04-16T00:00:00.000Z',
'workouts': [],
},
];
notice that some days have an empty list since there are not workouts for that date, in some days I have two or more workouts and in other days just one.
how can I achieve this? Im stuck here, any help is welcome thanks!!
CodePudding user response:
A naive approach would be to simply map()
the dates array and filter()
the workouts for each date.
const dates = ['2022-04-10T00:00:00.000Z', '2022-04-11T00:00:00.000Z', '2022-04-12T00:00:00.000Z', '2022-04-13T00:00:00.000Z', '2022-04-14T00:00:00.000Z', '2022-04-15T00:00:00.000Z', '2022-04-16T00:00:00.000Z', '2022-04-17T00:00:00.000Z'];
const workouts = [{ "id": "221bf3b1-2728-41f7-a10e-d004fdd1d0b6", "title": "ONE MORE TIME", "description": "Complete for time:\n\n3 rounds of:\n\n•40 Handstand Push-up\n•20 Push press\n\n♂ 135lb ♀ 95lb", "scheduledAt": "2022-04-10T00:00:00.000Z", }, { "id": "3323d0f6-50c9-48cd-9112-540580de0a33", "title": "DANGER!", "description": "3 Sets\nAMRAP 8 minutes of:\n\n•20 DB Thrusters\n•10 Pull-Ups\n•30 DB Squats\n\nRest 3 minutes between Amrap \n\n♂ 65 ♀ 45lb", "scheduledAt": "2022-04-11T00:00:00.000Z", }, { "id": "3af2e7d6-6efd-42ad-a09c-72e0022d6ce7", "title": "Fran", "description": "Complete for time:\n\n21 - 15 - 9\n\n•Thruster\n•Pull-ups\n\n♂ 95lb ♀ 65lb", "scheduledAt": "2022-04-12T00:00:00.000Z", }, { "id": "4945e72b-0fe5-4779-8f3d-9fdcd53fd406", "title": "Kabod Tuesday", "description": "AMRAP 20minutes of:\n\n•30 Hang Squat Clean\n•30 Toes-to-Bar\n•20 Thruster\n•20 Chest-to-Bar\n•10 Cluster\n•10 Muscle Up\n\n♂ 135 ♀ 95lb", "scheduledAt": "2022-04-13T00:00:00.000Z", }, { "id": "5c89574f-ea98-40ef-8e5f-ae38dfbad213", "title": "Squat Clean 5 sets", "description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps", "scheduledAt": "2022-04-13T00:00:00.000Z", }, { "id": "7b36c232-aac3-4c96-bc15-d8d523519944", "title": "Squat Clean 6 sets", "description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps\n#6: 1 reps", "scheduledAt": "2022-04-14T00:00:00.000Z", }];
const result = dates.map(date => ({
date,
workouts: workouts.filter(workout => date === workout.scheduledAt)
}));
console.log(result);
A more efficient approach requiring only a single iteration over the workouts
array would be to group the workouts by date, and then access the group in mapping over the dates
array.
const dates = ['2022-04-10T00:00:00.000Z', '2022-04-11T00:00:00.000Z', '2022-04-12T00:00:00.000Z', '2022-04-13T00:00:00.000Z', '2022-04-14T00:00:00.000Z', '2022-04-15T00:00:00.000Z', '2022-04-16T00:00:00.000Z', '2022-04-17T00:00:00.000Z'];
const workouts = [{ "id": "221bf3b1-2728-41f7-a10e-d004fdd1d0b6", "title": "ONE MORE TIME", "description": "Complete for time:\n\n3 rounds of:\n\n•40 Handstand Push-up\n•20 Push press\n\n♂ 135lb ♀ 95lb", "scheduledAt": "2022-04-10T00:00:00.000Z", }, { "id": "3323d0f6-50c9-48cd-9112-540580de0a33", "title": "DANGER!", "description": "3 Sets\nAMRAP 8 minutes of:\n\n•20 DB Thrusters\n•10 Pull-Ups\n•30 DB Squats\n\nRest 3 minutes between Amrap \n\n♂ 65 ♀ 45lb", "scheduledAt": "2022-04-11T00:00:00.000Z", }, { "id": "3af2e7d6-6efd-42ad-a09c-72e0022d6ce7", "title": "Fran", "description": "Complete for time:\n\n21 - 15 - 9\n\n•Thruster\n•Pull-ups\n\n♂ 95lb ♀ 65lb", "scheduledAt": "2022-04-12T00:00:00.000Z", }, { "id": "4945e72b-0fe5-4779-8f3d-9fdcd53fd406", "title": "Kabod Tuesday", "description": "AMRAP 20minutes of:\n\n•30 Hang Squat Clean\n•30 Toes-to-Bar\n•20 Thruster\n•20 Chest-to-Bar\n•10 Cluster\n•10 Muscle Up\n\n♂ 135 ♀ 95lb", "scheduledAt": "2022-04-13T00:00:00.000Z", }, { "id": "5c89574f-ea98-40ef-8e5f-ae38dfbad213", "title": "Squat Clean 5 sets", "description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps", "scheduledAt": "2022-04-13T00:00:00.000Z", }, { "id": "7b36c232-aac3-4c96-bc15-d8d523519944", "title": "Squat Clean 6 sets", "description": "Squat Clean For Load:\n\n#1: 3 reps\n#2: 3 reps\n#3: 1 reps\n#4: 1 reps\n#5: 1 reps\n#6: 1 reps", "scheduledAt": "2022-04-14T00:00:00.000Z", }];
const workoutsByDate = {};
for (const workout of workouts) {
(workoutsByDate[workout.scheduledAt] ??= []).push(workout);
}
const result = dates.map(date => ({
date,
workouts: workoutsByDate[date] ?? []
}));
console.log(result);