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.

⏱ 25 min

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.

Try it

Your first list

Console
Nothing in the console yet. Use console.log(...) to show values here.

Three things to remember:

  • Each item has a position, and counting starts at 0. cafes[0] is the first one, cafes[1] the second.
  • .length tells 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:

  1. Add one more cafe with push.
  2. 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:

Try it

One by one

Console
Nothing in the console yet. Use console.log(...) to show values here.

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 { }.

Try it

One cafe, several fields

Console
Nothing in the console yet. Use console.log(...) to show values here.

You read a field with a dot: cafe.name. And you change it the same way: cafe.open = false.

Challenges:

  1. Add a field rating: 4.5 to the object and show it with console.log(cafe.rating).
  2. Try showing cafe.phone, a field that doesn’t exist. The result is undefined: 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.

Exercise

Your favorite cafe

Create an object that describes your favorite cafe, add a field after the object is created, and build a sentence by reading the fields with a dot.

JavaScript
Tests
Click Run tests to check your code.
Console
Nothing in the console yet. Use console.log(...) to show values here.

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.

Try it

Only the open cafes

Console
Nothing in the console yet. Use console.log(...) to show values here.

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:

  1. Add a fourth cafe to the list, open, 0.4 km away.
  2. Change the if to show only the cafes less than 0.5 km away: if (cafe.km < 0.5).
  3. 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.

Exercise

Open and nearby

Finish openNearby. It takes a list of cafes and returns a new list with only the names of the ones that pass the test. The tests also call the function with other lists, so use the list parameter, not the cafes variable.

JavaScript
Tests
Click Run tests to check your code.
Console
Nothing in the console yet. Use console.log(...) to show values here.

Counting starts at 0

Find the bug

This code should show all three cafes, but it skips the first one and shows undefined at the end. Which line has the problem?

Click the line with the problem.

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, and lng: 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 for creates eight rings and adds each one with rings.push(ring). Then, on every frame, a for (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 a name, portion, calories, and more. The app goes through the list to build the table and add up the calories.
Task

Practice

0 of 6 done

Checkpoint

Check what you learned

  1. In const cafes = ['A', 'B', 'C'], what is cafes[0]?

  2. What is cafes.length for that same list ['A', 'B', 'C']?

  3. What does this code show?

    const cafe = { name: 'Torra Fina', km: 0.96 };
    console.log(cafe.name);
  4. And what if it were console.log(cafe.Name), with a capital N?

Finished the lesson?

Mark it complete to track your progress.