1

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.

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.

5
  • 1
    It doesn't matter that the food objects are held in another array, your foodplan should just be an array, exactly like the food one you've got there Commented Sep 8, 2018 at 18:16
  • @user184994 and what methods can I use, to get specific objects from the food array? - The problem here is, that I need 5 different foodplans (by adding weeknumber) for 5 weeks, how can I handle this? Commented Sep 8, 2018 at 19:33
  • It depends what you want to do really. You can get one out using it's index for example, using this.food[0] would get the first one, or you can use the find function to get one, like this.food.find((f) => f.id === 12) would return the one with id 12 Commented Sep 8, 2018 at 19:35
  • Foodplan needs the Attributes: FoodPerWeek, where 5 food objects are in and WeekNumber Foodplan needs methods to showFood, addFood, changeFood and deleteFood. Commented Sep 8, 2018 at 19:39
  • So you can use push to add an item to an array. this.foodPlan.foodPerWeek.push(this.food[0]) would push a copy of the first item in food, and place it in foodPerWeek Commented Sep 8, 2018 at 19:41

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

hey. thanks first, will it be possible to add functions with your solution like add/delete/change foodplans ?
uff. I'm having a hard time, implementing your code in Angular6/Typescript... the structure just needs to be different.
I can't do your homework, I'm sorry. You will have to try by yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.