Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda