Filter gym sessions and merge duration and equipment per user
You are tasked with developing a function capable of selecting and filtering gym session data based on specific options. Each session is detailed within an array of objects, highlighting the user, duration of the exercise, and the equipment utilized. A crucial feature of this function is its ability to consolidate sessions by the same user, thereby amalgamating their total exercise duration and equipment used. Array.prototype.reduce() is a natural fit for grouping the gym sessions.
Input:
sessions: An array of session objects, each containing user, duration, and equipment properties.options: An object hosting filtering and merging preferences.Output:
An array of session objects tailored according to the specified options, inclusive of user-merged data if indicated.
Example:
const input = [
{ user: 'User1', duration: 30, equipment: ['treadmill'] },
{ user: 'User2', duration: 45, equipment: ['bike', 'dumbbell'] },
// additional session objects
];
const options = { merge: true, equipment: ['bike'] };
const selectedSessions = selectData(input, options);
console.log(selectedSessions);
// Output might resemble:
// [
// { user: 'User2', duration: 45, equipment: ['bike', 'dumbbell'] }
// ]
This function not only demands adeptness in data filtering based on criteria but also challenges you to thoughtfully combine data from multiple entries pertaining to the same individual, ensuring a seamless and informative summary of gym sessions.