Using Objects
We want to use objects in situations where we want to associate one thing with another. For example, in the previous section, we associated words with definitions using an object. Now let's explore other situations where objects would be useful.
Let's say that we want to create a menu for a restaurant. Thus, we want to associate items on the menu with their respective prices. We can use the code snippet below to create a menu object and add soda, hot dogs, hamburgers, and french fries.
Now, our menu looks something like this:
ITEM
PRICE
Soda
1
Hot dog
2
Hamburger
3
French fries
2
Printing out the menu
We want to print out our menu to the console. Of course, we can print out our menu one by one. For example, we could do this:
Printing out our menu one item at a time takes a lot of effort, and the code would have to change whenever we want to make a change to our menu. A better way of printing out our menu would be to loop through our menu.
How would we accomplish this? For loops would not work, since we cannot use an integer to access values in our menu. A while loop would not work either, since there is no condition that we could work with. Thus, to loop over an object, we have no choice but to use a new type of loop, a for-each loop.
A for-each loop allows us to run a segment of code for each key in an object. A for each loop is structured like so:
The first blank will contain a name for the keys in the object and the second blank will contain the object that you want to loop over. For example, if we wanted to print out the items in our menu, we would do so like this:
We would read this loop as "for each item in the menu, print out the item". Thus, this loop would print out:
As you can see, we are already almost done printing out our menu. All we need to do is add the prices to the items. Since we are looping through the keys of an object, we can use the keys to look up their respective values. Using what we learned from the last section, we know that we can look up the prices of our items using:
We can easily change our print statement so that it contains the price.
Once we put everything together, this is the resulting code.
The code above will print out the following menu to the console.
Last updated
Was this helpful?