Consider I have this array of objects:
const workouts = [
{
workoutId: 1,
username: "John Doe",
exercises: [
{
exerciseId: 10,
sets: [
{
setId: 100
},
{
setId: 101
},
// more set objects
]
},
{
exerciseId: 11,
sets: [
{
setId: 102
},
// more set objects
]
},
// more exercise objects
]
},
{
workoutId: 2,
username: "James Smith",
exercises: [
{
exerciseId: 12,
sets: [
{
setId: 103
}
]
}
]
},
// more workout objects
]
I need to delete a specific object (set) from this array, depending on two values passed from the user. These values will be a exerciseId and a setId. As the actual array from my project is larger than this example, I also get the context of a user to narrow down the search criteria (hopefully improving the speed at which it executes?). I currently have this function:
function deleteSet(workouts, exerciseId, setId, context) {
const { username } = checkAuth(context) // Checks for logged in user and gets the username from it.
const userWorkouts = workouts.find( { username: username });
}
So far, I know that userWorkouts will retrieve all the workouts with the logged in user's username and store it as an array. Consider that setId = 101
and exerciseId = 10
, how can I achieve only deleting the set object that matches these two inputs from the userWorkouts array?