Chapter 3 · Data

Objects

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

An object holds values by name

By the end of this lesson you keep records, loop over them, and save them as text.

const p = { name: "Ada", age: 36 };
const key = "age";
  • An object holds values by name. Each property is a key and its value.
  • p.name is "Ada". p[key] reads the key in the box key: 36.
  • A key with a space needs quotes: { "first name": "Ada" }, read as x["first name"].
  • On the heap, as an array: the box holds an arrow.
const p = { name: "Ada" };
p.age = 36;
delete p.name;
console.log(p.name);
console.log(p.city ?? "none");
console.log(p);
const prices = { tea: 2.5, cake: 3 };
console.log(Object.keys(prices).join(", "));
for (const [key, value] of Object.entries(prices)) {
  console.log(`${key}: ${value.toFixed(2)}`);
}
const s = {};
s.Zoe = 3;
s["10"] = 1;
s.Ann = 2;
console.log(Object.keys(s).join(" "));
const prices = { tea: 2.5, cake: 3, juice: 1.75 };
let total = 0;
// log each item and its price; add it to total
console.log(`Total: ${total.toFixed(2)}`);
const a = { score: 1, tags: ["x"] };
const b = a;
const c = { ...a };
b.score = 2;
c.tags.push("y");
console.log(a.score, c.score);
console.log(a.tags.join(","));
const text = `[{"name":"Ada","score":90},
  {"name":"Alan","score":85}]`;
const people = JSON.parse(text);
for (const p of people) {
  const { name, score } = p;
  console.log(`${name}: ${score}`);
}
function record(name, score) {
  return { name, score };
}
people.push(record("Grace", 95));
console.log(JSON.stringify(people[2]));
import { readFileSync } from "node:fs";
const text = readFileSync(0, "utf8");
// 1. parse the records
// 2. log each one: Alan: 85
// 3. log the best: Best: Ada
const ada = { name: "Ada", tags: ["math"] };
const copy = { ...ada };
copy.tags.push("code");
console.log(ada.tags.join(", "));
console.log(copy.tags.join(", "));
Terminal
Step 1 of 8Add, delete, and what is missing

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)