1

I've built the class "meal" with two properties like in the tour of heroes tutorial from angular:

export class Meal {
  id: number;
  name: string;
}

Now I want to declare a class like "Mealplan". Each Mealplan should contain five Objects of Meal, like:

export class Mealplan {
  weekId: number;
  mealPlan: Meal[] = new Array(5)
}

Important is, that I want to add existing Meals to the Mealplan. I do not want to create new Objects of Meal. Is it more recommended to just refer to the Meal ID in Mealplan? For example:

  createDb() {
    const meal = [
      {
        id: 101,
        name: 'Tatar',
        price: 18.00,
        description: "Sojasauce / Chili / Crème fraîche / Tobiko"
      };

and

const mealPlan = [
 {
  id: 1,
  mealPlan: [101, 101, 101, 101, 101]
 }
]

Is this the best way I can handle this problem? Or would it be better to use a Map or List or something else?

Cenasa
  • 531
  • 9
  • 27

1 Answers1

2

You could associate a type to mealPlan like this:

export class Mealplan {
  weekId: number;
  mealPlan: [Meal, Meal, Meal, Meal, Meal];
}

Now, doing this would be invalid:

let plan = new MealPlan();
plan.mealPlan = [new Meal(),new Meal(),new Meal(),new Meal()];

whereas this is valid:

let plan = new MealPlan();
plan.mealPlan = [new Meal(),new Meal(),new Meal(),new Meal(), new Meal()];
storm85049
  • 31
  • 4
  • hey there. thanks for your comment, but I dont think I should create new objects inside a mealplan. I have edited my opening post, do you have another idea? – Cenasa Jan 18 '20 at 00:03
  • I have created a little example for you https://stackblitz.com/edit/json-pipe-example-4vbixg – storm85049 Jan 18 '20 at 10:02
  • thanks for your effort! I understand your point, and I think it's a great solution. However, I need to figure out how I do it with the in-memory-db. PS: I'm from hamburg too! :D @storm85049 – Cenasa Jan 18 '20 at 12:29