简体   繁体   English

如何使用嵌套项的动态数量创建 model TypeScript

[英]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.每个锻炼都有嵌套练习,例如: 锻炼名称:“第 1 天” 锻炼:俯卧撑、深蹲等。

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.所以,我想创建一个包含这些练习的锻炼 model。 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?锻炼 model 应该是什么样子?

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?您实际遇到的问题是什么/您的问题是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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