Loading video player…

Comparing Records by Identity

In this lesson, you started porting the co-op’s Python check to JavaScript, with the line that skips resent readings. In the 09:00 batch, the ridge station resent its reading over flaky Wi-Fi, so the batch holds two separate dicts with the same fields. Python’s if reading in seen: compares with ==, so the copy counts as seen, and the check prints 3 readings checked.

  • A line-by-line port, if (seen.includes(reading)) {, lets the resent reading through: JavaScript prints the ridge line twice, and the count says 4 readings checked.
  • Python asks two questions: == (do two objects hold the same contents?) and is (are they the same object?). For the two ridge dicts, == is True and is is False.
  • For objects, JavaScript’s includes() compares the way === does, and === asks Python’s is question. The resent reading is a second object, so === gives false. JavaScript writes its booleans in lowercase, true and false.
  • Names point at shared objects, as in Python: search for the first reading itself, and includes() finds it. Comparing two readings with === or !== follows the same rule, so equal fields don’t make them the same object.
  • The fix is to decide what makes two readings the same, here the station and its sequence number, and keep the names built from them, such as ridge #1412, in seen, as a Python check could too.
  • Strings compare by value however they were built, so "rid" + "ge" === "ridge" is true. In Python, a string is an object, so is could give False for equal text, and you compare with ==. In JavaScript, there’s no identity question to ask of a string: === compares the characters.
  • Read === as Python’s == for numbers, strings and booleans, and as Python’s is for arrays and objects. Copying objects, and comparing every field of two whole objects, are a later topic.

Here’s the port’s loop with the fix in place. The field checks and each reading’s line are left out:

Language: JavaScript
const seen = [];
let checked = 0;
for (const reading of batch) {
  const name = `${reading.station} #${reading.seq}`;
  if (seen.includes(name)) {
    console.log(`Skipped resent ${name}`);
  } else {
    seen.push(name);
    // ... reject bad fields, or print the line and count it
  }
}

console.log(`${checked} readings checked`);

When you run the whole check with node readings-check.js, it prints the same six lines as the Python check:

Language: Program Output
ridge #1412: 6.5°C, 0.2 mm rain, at 54.61, -3.05
mill #877: 7.4°C (unchanged), dry, at 54.58, -3.11
Skipped resent ridge #1412
orchard #2290: no temperature (unchanged), no rain data, no GPS
Rejected school #96: bad fields
3 readings checked

Next up is the text lesson Why Strings and Numbers Can’t Change. There, you’ll see why === can compare strings by value, and how a string can have methods at all.

00:00 In this lesson, you’ll start porting the co-op’s Python check to JavaScript, with the line that skips resent readings. On the way, you’ll find out what JavaScript’s equality check really compares.

00:11 The 09:00 batch has five readings. The ridge reading arrives twice, because the station resent it over flaky Wi-Fi. Both copies have the same fields, but they’re two separate dicts.

00:27 In the Python check, seen starts empty. For each reading, the if line asks whether an equal reading came through already. Python’s in compares with ==, so equal fields are enough.

00:40 If it’s new, the check adds it to seen, then checks its fields. Python prints the ridge line once and skips the copy. The last line counts three readings.

00:55 The port carries most lines over as you’d expect. seen is an array held in a const, and the counter is a let variable.

01:03 The loop becomes a for...of loop, the name a template literal, and each print() a console.log() call. The if line becomes seen.includes(reading), with braces around each block.

01:16 Like Python’s in, it asks whether the array holds a value. And append() turns into push() below it. Each reading is an object now, with the same fields as the dict.

01:27 The first ridge reading is already in seen, and the resent one looks exactly like it. Does includes() find it? Your turn: pause the video and predict.

01:43 No: it misses it.

01:46 JavaScript prints the ridge line twice, and the count says four readings checked. Python skipped that copy, and the port let it through.

01:57 Why does includes() miss it? Python asks two different questions. == asks whether two objects hold the same contents. is asks whether they’re the same object.

02:11 For the two ridge dicts, == says True for the contents, and is says False for the objects. The in check uses ==, so Python finds the copy.

02:23 JavaScript’s includes() compares objects the way === does. And === on objects asks the question Python’s is asks: is this the same object?

02:36 The resent reading is a second object, built from its own braces. It points somewhere else, so === says false, in lowercase, as JavaScript writes its booleans.

02:48 Names still point at shared objects, as in Python. Ask for the first reading itself, and includes() finds it. Comparing two readings with === or !== follows the same rule: equal fields don’t make them the same object.

03:06 So what makes two readings the same? Here, it’s the station and its sequence number, and the name already combines the two. The fix keeps the names in seen instead of the readings, as a Python check could too.

03:21 The if line searches for the name, and push() adds the name. Run the port, and JavaScript skips the resent copy. The count says three, and all six lines match Python’s.

03:35 Each name is a new string, built by the template literal. It’s still found, because strings compare by value, however they were built. Joining "rid" and "ge", for example, gives a string that’s === to "ridge" anyway.

03:50 In Python, a string is an object, so is could give False for equal text, and you compare with == instead. In JavaScript, there’s no identity question to ask of a string: === compares the characters.

04:07 Read === as Python’s == for numbers, strings, and booleans. For arrays and objects, read it as Python’s is question.

04:18 To ask whether two records are the same, decide which fields make them the same, and compare those, or a name built from them. Copying objects, and comparing every field of two whole objects, are a later topic.

04:33 Next up is Why Strings and Numbers Can’t Change, a text lesson. There, you’ll see why === can compare strings by value, and how a string can have methods at all.

Become a Member to join the conversation.