From Zero to Deploy · Module 2 · Lesson 2.4
2.4 Lists and objects, organizing the data
How to keep several values in a list, describe one thing with an object, and go through a list of objects, the most common shape of data there is.
A list keeps several values in order
A variable keeps one value. But a cafe app has many cafes. That’s what a list is for (the AI calls it an array): several values in order, inside square brackets [ ], separated by commas.
Three things to remember:
- Each item has a position, and counting starts at 0.
cafes[0]is the first one,cafes[1]the second. .lengthtells you how many items the list has..push(...)adds a new item at the end.
Notice that the list was created with const and still got a new item. const makes sure the box always holds the same list, but the list itself can grow.
Challenges:
- Add one more cafe with
push. - Show the last cafe, whatever the size of the list:
console.log(cafes[cafes.length - 1]). Why the- 1?
for…of: for each item in the list
In the last lesson, for counted numbers. To go through each item of a list, there’s a shorter way, for...of:
Challenge: number the cafes. Before the for, create let number = 1. Inside, show console.log(`${number}. ${cafe}`) and then add 1: number = number + 1.
An object describes one thing
A cafe has a name, a distance, and it can be open or closed. To group everything about one thing, there’s the object: field: value pairs inside braces { }.
You read a field with a dot: cafe.name. And you change it the same way: cafe.open = false.
Challenges:
- Add a field
rating: 4.5to the object and show it withconsole.log(cafe.rating). - Try showing
cafe.phone, a field that doesn’t exist. The result isundefined: it’s not an error, just “there’s nothing here”. A misspelled field name gives the same result, and it’s a common bug.
Now it’s your turn. Write the code in the editor and click Run tests.
A list of objects
Put the two ideas together and you get the shape of data you’ll see most in your life: a list of objects, all with the same fields. A list of cafes, of rings, of the foods in a photo.
cafes[1].name means: in the list cafes, take the item at position 1, and from it take the field name. And for...of with an if inside goes through all of them and picks only the open ones.
Challenges:
- Add a fourth cafe to the list, open, 0.4 km away.
- Change the
ifto show only the cafes less than 0.5 km away:if (cafe.km < 0.5). - Now only the ones that are open and less than 0.5 km away. You already know how to join two conditions.
When an app asks a server for data, it almost always arrives like this, in a format called JSON, which is written just like lists and objects in JavaScript.
Counting starts at 0
Where you’ll see this in the course
- In the map app, the places are a list of objects, each with an
id,name,lat, andlng: the same cafes as in this lesson. In the map module you’ll learn shorter ways to filter and sort lists like this, from nearest to farthest. - Also in the map app, the achievements are just data: a list of objects with a name, an emoji, and how many check-ins each one takes. To create a new achievement, you add an object to the list.
- In the 3D game, the rings live in a list: a
forcreates eight rings and adds each one withrings.push(ring). Then, on every frame, afor (const ring of rings)checks whether the plane flew through any of them. - In the AI app, the AI’s answer is an object with a list
items, and each item has aname,portion,calories, and more. The app goes through the list to build the table and add up the calories.
Mark it complete to track your progress.