Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Find a Record by Its Id, Not Its Position

In this video, you gave every game on your shelf an id and wrote a function that finds a game by its id instead of by its position.

Key Points

  • A record’s position belongs to the list and can shift when the list changes. An id is a field that stays with the record, like a sticker on a game box. Each game gets its own id, so one id points to one game.
  • console.table() shows both side by side. The (index) column is each record’s position, and the id column is its id. Catan sits at position 2, but its id is 7.
  • To find a record by its id, loop over the list and compare each record’s id field with the id you want. When they match, return that record right away, which also stops the loop. If no record matches, the loop runs to the end and the function returns undefined. If an id were ever repeated, the search would return the first record with it, which is why each id belongs to one record only.
  • A number in square brackets after the list is a position, while an id is a field you compare. That’s why games[2] gives you Catan, but findGame(games, 2) gives you Azul, whose id is 2.
  • The search returns the list’s own record, not a copy. When you change a field of the found record, the change shows up in the list too.

The Final Code

Language: JavaScript Filename: scratch/shelf.js
const games = [
  { id: 12, title: "Ticket to Ride", maxPlayers: 5, minutes: 60 },
  { id: 3, title: "Codenames", maxPlayers: 8, minutes: 15 },
  { id: 7, title: "Catan", maxPlayers: 4, minutes: 90 },
  { id: 2, title: "Azul", maxPlayers: 4, minutes: 45 },
];

function findGame(games, id) {
  for (const game of games) {
    if (game.id === id) {
      return game;
    }
  }
  return undefined;
}

console.log(games[2].title);
console.log(findGame(games, 2).title);

const found = findGame(games, 7);
found.minutes = 120;
console.table(games);

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

00:00 You’ve written functions that report on a playlist. Now it’s back to your game shelf. How do you pick out one particular game? So far, you’ve reached a game by its position.

00:11 games[2] gives you Catan. But a position belongs to the shelf, not to the game. Lend a game to a friend or squeeze a new one in, and the positions can shift.

00:23 So you give every record one more field, an id. Each game gets its own id, and it stays with the game, wherever the game stands. Think of it as a sticker on the box.

00:35 Here’s the shelf as a table. The index column shows each game’s position, and the id column shows its sticker. Catan sits at position two, but its id is seven.

00:48 Say your group votes on tonight’s game by sticker number, and game seven wins. You want the record with that id, wherever it stands. That calls for a search.

00:59 The function findGame() takes the shelf and the id you’re looking for. Inside, a loop hands you each game record in turn. On each pass, it compares that game’s id with the id you asked for.

01:14 When they match, it returns that game right away, and the loop stops. So if two games ever shared an id, you’d only get the first one. If no game has that id, the loop runs to the end, and the function returns undefined instead of a record.

01:33 Now call it with seven. The first pass compares twelve with seven. No match. The second pass compares three with seven. No match either. The third pass compares seven with seven.

01:46 That’s a match. So the function returns Catan’s whole record, and Azul never gets checked. Run the script, and the terminal shows the record with id seven.

01:59 Ids are often small numbers, and small numbers look a lot like positions. Look at these two lines. Your turn. Pause the video and predict whether both lines give you the same game.

02:17 No, they don’t. The first line counts positions, so it gives Catan, the third game on the shelf. The second compares ids, so it gives Azul, whose id is two.

02:30 So when you meet a number, ask whether it’s an id or a position. Square brackets after the list mean a position. An id is a field you compare.

02:42 Finding a game by its id gives you something more. Your group always plays Catan slower than the box says, so you want to store 120 minutes. You keep the game with id seven in a new variable, found.

02:56 Then you set its minutes to 120. Now print the whole shelf as a table. Catan’s row says 120.

03:06 The function didn’t hand you a copy. It returned the shelf’s own record, so a change through found shows up on the shelf too. Why that works is a topic for a later course.

03:16 For now, a found record is the real one.

03:21 Here’s the rule: find a record by comparing its id, and you get the list’s own record back. In the next exercise, Find and Update a Library Loan, you’ll write a search like this for library loans and update the loan you find.

Become a Member to join the conversation.