Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Converting a list of objects to a different list of objects in javascript

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) {
    .....
  }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!