Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda