Keep a List of Records as a Table

Your game record can now answer questions about one game. It knows how many people can play and how long a round takes, and you can compare those fields with your group’s size and the time you have available. But a game night rarely starts with one box. You stand in front of a whole shelf and want to know which games fit tonight.

In this lesson, you’ll keep several game records together in one array and treat it like a table. You’ll read one field of one record, then loop over all the records to print, total, count, and pick out games. By the end, you’ll write small functions that take the whole shelf and return an answer, such as the games that four friends can finish in an hour.

Put Several Records in One Array

You already know how to keep many values in an array, and you know how to describe one game as a record. An array can hold records just like it holds numbers or strings. Create scratch/shelf.js and type in a shelf of four games:

Language: JavaScript Filename: scratch/shelf.js
const games = [
  { title: "Ticket to Ride", maxPlayers: 5, minutes: 60 },
  { title: "Codenames", maxPlayers: 8, minutes: 15 },
  { title: "Catan", maxPlayers: 4, minutes: 90 },
  { title: "Azul", maxPlayers: 4, minutes: 45 },
];
console.log(games.length);
console.log(games[1]);

Each record is enclosed in curly braces and has the same three fields you used before. The commas between the records separate the array’s items, exactly as they do in an array of numbers. Before you run it, decide what the two lines print. Then run the script:

Language: Shell
$ node scratch/shelf.js
4
{ title: 'Codenames', maxPlayers: 8, minutes: 15 }

The array has four items, and each item is a whole record. Position 1 holds the second game, Codenames, because positions still count from zero.

This list of records works like a table. Each record is a row that describes one game, and each field name labels a column that every row shares. Node.js can even draw it that way for you. Replace the two logging lines with a call to console.table():

Language: JavaScript Filename: scratch/shelf.js
const games = [
  { title: "Ticket to Ride", maxPlayers: 5, minutes: 60 },
  { title: "Codenames", maxPlayers: 8, minutes: 15 },
  { title: "Catan", maxPlayers: 4, minutes: 90 },
  { title: "Azul", maxPlayers: 4, minutes: 45 },
];
console.table(games);

Run the script to see the table:

Language: Shell
$ node scratch/shelf.js
┌─────────┬──────────────────┬────────────┬─────────┐
│ (index) │ title            │ maxPlayers │ minutes │
├─────────┼──────────────────┼────────────┼─────────┤
│ 0       │ 'Ticket to Ride' │ 5          │ 60      │
│ 1       │ 'Codenames'      │ 8          │ 15      │
│ 2       │ 'Catan'          │ 4          │ 90      │
│ 3       │ 'Azul'           │ 4          │ 45      │
└─────────┴──────────────────┴────────────┴─────────┘

The (index) column shows each record’s position in the array, and the other columns come from the field names. You won’t need console.table() to solve anything in this course. It’s an optional tool from console that you can reach for whenever you want to scan a list of records.

Your shelf will grow too. To add a game, use push() just as you would with any other array, and pass it a new record. Try games.push({ title: "Uno", maxPlayers: 10, minutes: 20 }); before the console.table() line and run the script again to see a fifth row.

Read One Field of One Record

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.