Updating a Map With set() and delete()
Ola Bakare, member 318, has applied for a plot, and plot 3 goes to the first person on the waiting list, who then leaves it. In this lesson, you ported Rosa’s two changes to the waiting list, which is a Map, and saw why the brackets you’d use on an object don’t change it.
Here’s the step in the Python script:
GIVEN_UP = 3
waiting = {
214: "Dev Patel",
187: "Hannah Cole",
305: "Marek Nowak",
42: "June Harris",
}
waiting[318] = "Ola Bakare"
member = next(iter(waiting))
name = waiting.pop(member)
names = ", ".join(waiting.values())
print(f"Plot {GIVEN_UP} goes to {name} (member {member})")
print(f"Waiting list: {names}")
- Python prints
Plot 3 goes to Dev Patel (member 214)andWaiting list: Hannah Cole, Marek Nowak, June Harris, Ola Bakare. - On an object, square brackets add a key and the
deleteoperator removes one, so a line-by-line port adds Ola withwaiting[318] = "Ola Bakare";. A Map has nopop(), so the port reads the name withwaiting.get(member)and then writesdelete waiting[member];. - The port runs without an error, and its first line matches Python’s. But the second is
Waiting list: Dev Patel, Hannah Cole, Marek Nowak, June Harris: Dev Patel got the plot and kept his place, and Ola Bakare never joined the list. - A Map answers Python’s
318 in waitingwithwaiting.has(318)andlen(waiting)withwaiting.size, as a Set does.sizeis a property, so it takes no parentheses. The port’s Map has a size of 4, the old list unchanged, andhas(318)isfalse. - Logging the Map shows the four old entries, each with
=>, and after them'318': 'Ola Bakare', with a colon where the entries have an arrow, and a string key. That’s how Node logs an ordinary property, not an entry. - Like a dict’s items and attributes, a Map’s entries are apart from its properties, as the last lesson said, in a store that only its methods reach, such as
get(),has()and thekeys()you spread. - On a Map, square brackets reach properties, so they miss the store:
waiting[318] = "Ola Bakare"wrote a property under the string'318', as objects store number keys, anddelete waiting[214]looked for a property named'214', found none, still returnedtrue, where Python’sdelraises aKeyError, and changed nothing.
Here’s the port with the Map’s own methods:
const GIVEN_UP = 3;
const waiting = new Map([
[214, "Dev Patel"],
[187, "Hannah Cole"],
[305, "Marek Nowak"],
[42, "June Harris"],
]);
waiting.set(318, "Ola Bakare");
const member = [...waiting.keys()][0];
const name = waiting.get(member);
waiting.delete(member);
const names = [...waiting.values()].join(", ");
console.log(`Plot ${GIVEN_UP} goes to ${name} (member ${member})`);
console.log(`Waiting list: ${names}`);
console.log(waiting.size, waiting.has(318));
set() takes a key and a value and adds the entry. delete() takes a key and removes its entry, and it returns true if it removed an entry and false if not, never the value, so get() reads the name first: together, they do the work of Python’s pop(). The port now prints Python’s lines, and the checks agree:
Plot 3 goes to Dev Patel (member 214)
Waiting list: Hannah Cole, Marek Nowak, June Harris, Ola Bakare
4 true
set() on a key that’s already there replaces the value and keeps its place, as assigning to a dict key does, so waiting.set(187, "Hannah Cole-Byrne") leaves her first. A Map also keeps 318 a number, so waiting.has("318") is false, as Python’s "318" in waiting is False.
The rule to keep: a Map’s entries are reached only through its methods and size. set() adds or replaces, delete() removes, get() reads, has() checks, and size counts. Keys in square brackets are for objects.
Next up is the text lesson Choosing Between a Map and an Object, where you’ll decide when data belongs in an object and when in a Map, and how to get a Map into JSON and back.