[英]Typescript: Array of Objects of another Array (Angular6)
I have this array of "food": 我有这一系列的“食物”:
"food": [
{
"id": 11,
"name": "Kabeljaufilet",
"preis": 3.55,
"art": "mit Fisch"
},
{
"id": 12,
"name": "Spaghetti Bolognese",
"preis": 3.85,
"art": "mit Fleisch"
},
{
"id": 13,
"name": "Pizza Salami",
"preis": 3.99,
"art": "mit Fleisch"
},
Now I need another Array called "foodplan", where I can add, delete etc. foods from the first array. 现在,我需要另一个名为“ foodplan”的数组,在这里可以从第一个数组添加,删除等食物。
I have never created Arrays where Objects of another Arrays were implemented. 我从未创建过实现其他数组对象的数组。 How to go on now?
现在如何进行?
Foodplan needs the Attributes: FoodPerWeek , where 5 food objects are in and WeekNumber Foodplan needs methods to showFood, addFood, changeFood and deleteFood. Foodplan需要以下属性: FoodPerWeek ,其中有5个食物对象,WeekNumber Foodplan需要显示食物,添加食物,更改食物和删除食物的方法。
This is just a basic example. 这只是一个基本示例。 Of course you could dynamically resize the array if you prefer.
当然,您可以根据需要动态调整数组的大小。 There need to be some checks to validate the input, etc. I leave this to you.
需要进行一些检查以验证输入等。我将其留给您。 Have fun.
玩得开心。
enum WeekDay {
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4
}
class Food {
public id: number
public name: string
public preis: number
public art: string
}
class FoodPlan {
private weeklyFood: Food[] = new Array<>(5)
addFood(food: Food, weekDay: WeekDay) {
this.weeklyFood[weekDay] = food
}
showFood(weekDay: WeekDay) {
console.log(this.weeklyFood[weekDay])
}
remove(weekDay: WeekDay) {
this.weeklyFood[weekDay] = null
}
}
let foodPlan: FoodPlan = new FoodPlan()
let firstFood: Food = new Food()
firstFood.id = 1
firstFood.name = "Kabeljaufilet"
firstFood.preis = 3.55
firstFood.art = "mit Fisch"
foodPlan.addFood(firstFood, WeekDay.Wednesday)
foodPlan.showFood(WeekDay.Wednesday)
foodPlan.remove(WeekDay.Wednesday)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.