I'm having a problem that I'm having a hard time with. I have a list of exercises objects like below:
exerciseDataList =
[
{exerciseType: 'Legs', activity: 'Squats', weight: '180', reps: '10'},
{exerciseType: 'Legs', activity: 'Squats', weight: '180', reps: '10'},
{exerciseType: 'Legs', activity: 'Leg Press', weight: '150', reps: '10'},
{exerciseType: 'Legs', activity: 'Leg Press', weight: '140', reps: '10'},
{exerciseType: 'Chest', activity: 'Bench Press', weight: '180', reps: '10'},
]
I'm hoping to turn the above to below:
treeData =
[
{
'name':'Legs',
'data': [{'x':'Squats','y':3600}, {'x':'Leg Press', 'y':2900}]
},
{
'name':'Chest',
'data': {'x': 'Bench Press', 'y': 1800}
}
]
Essentially data.x is the activity and data.y is the sum of weight * reps for the same exerciseType and activity.
I'm having a hard time wrapping my head around this and wondering what would be the efficient way to do this as the exerciseDataList can get long.
I know I need to loop through the list of exercise objects and treeData but this is when I'm not sure what to do next. I feel like to achieve this I need 3 for loops? How would I go about doing this and is there a better solution that O(n^3)? Thank you.
for (let edata of this.getExerciseDataList) {
for (let data of this.treeData) {
.....
}
}