Meal Maker project question

I’ve finished the project and redid it a couple of times to make sure I understand it. There’s one thing here that I can’t understand how it works though so if anyone could clarify this it would be amazing:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },

get courses(){
  return {
    appetizers: this._courses.appetizers,
    mains: this._courses.mains,
    desserts: this._courses.desserts,
  };
},

addDishToCourse(courseName, dishName, dishPrice){
  const dish = {name: dishName, price: dishPrice}
return this._courses[courseName].push(dish)
},

So, to access a particular array (appetizers, mains and desserts) in _courses, it’s used [courseName].

I’m a bit confused about this so I hope I make sense. How does [courseName] knows which particular array (appetizers, mains or desserts) to access?

In other words, when I type:

menu.addDishToCourse('appetizers','oninon rings', 7.44);

How does the computer knows that the data is to be stored in the appetizers array at the top, if in menu.addDishToCourse(), '“appetizers” is writthen as a string?

I hope it makes sense!

Hi Rui,

you find the answer in one of the preceding lessons:
Property Assignment

Take this example:

const obj = {
	prop: 'value'
}

This:

console.log(obj.prop) // --> "value"

logs the same as this:

console.log(obj['prop']); // --> "value"

This way you can pass a string either directly or via a variable as the key name:

const propertyName = 'prop';
console.log(obj[propertyName]); // --> "value"

While this:

const propertyName = 'prop';
console.log(obj.propertyName); // -->  undefined

looks for a key named propertyName.

I hope that answers your question and you did not want to know what happens deep down in the memory of your computer instead…

1 Like

Thank you so much, it made more sense now!

1 Like