简体   繁体   中英

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

I have a list of workouts. Each workout has nested exercises, eg: 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?

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} ]}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM