Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a model with dynamic amount of the nested items TypeScript

I have a list of workouts. Each workout has nested exercises, e.g:
Workout name: "Day 1"
Exercises: push-ups, squats, etc.

Users should be able to create new exercises and delete those they don’t need.
So, I want to create a Workout model that will include these exercises. The problem is you never know how many of these exercises will be there.

I think I just don’t fully understand the models. Could you advise, please?
How the workout model should look like?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My target is that after the user will add a new Workout with exercises I will be able to do something like:

export interface Workout {
  name: string,
  exercises: [ Exercise1, Exercise2, Exercise3, ... ]
}

export interface Exercise {
  name: string,
  status: boolean
}

newWorkout: Workout = {name: "Day 1", exercises: [ {name: "push-ups", completed: true}, {name: "squats", completed: false} ]}

>Solution :

Adding exercises as an array of Exercise in Workout should allow you to do that:

export interface Workout {
  name: string,
  exercises: Exercise[]
}

export interface Exercise {
  name: string,
  status: boolean
}

But please clarify what you mean by "The problem is you never know how many of these exercises will be there.". What is the problem you actually encountered / what is your question?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading