Rovers beat Albion 4-1 in week 9, but until the committee confirms it, Nadia’s script applies the score to a copy of the confirmed table. The intro’s port made that copy with [...table], so here’s exactly what that spelling copies and what it doesn’t. Shallow copies work the same way in Python and JavaScript, and only the spelling changes. Start with arrays and records.
Copy Arrays and Records With Three Dots
JavaScript usually spells a shallow copy of an array or a plain object as three dots inside a new literal. Here’s how the usual Python spellings map to JavaScript:
| Python | JavaScript |
|---|---|
table.copy(), list(table), table[:] |
[...table] |
dict(rovers), rovers.copy(), {**rovers} |
{ ...rovers } |
{**rovers, "points": 16} |
{ ...rovers, points: 16 } |
Arrays and objects have no copy() method, so a ported table.copy() throws TypeError: table.copy is not a function.
The new literal builds the container, and ... fills it. Array spread takes the items any iterable yields. Object spread copies only the object’s own enumerable properties—those set on it directly, such as team and points—and not its class or prototype. That’s why the {**rovers} row holds for plain records like these. See MDN on spread syntax for the edge cases.
For a dense array like this one, table.slice() makes the same copy. The two differ only on arrays with empty slots, which spread fills with undefined. Here are the top three rows of the confirmed table after week 8, before the result from week 9:
const table = [
{ team: "Athletic", played: 8, points: 15 },
{ team: "Rovers", played: 8, points: 13 },
{ team: "Wanderers", played: 8, points: 12 },
];
const provisional = [...table];
console.log(provisional.length, provisional === table);
console.log(provisional[0] === table[0]);
For objects and arrays, === checks identity, like Python’s is. Look for a different array on the first line and the same first record on the second:
3 false
true
The copy is a new array holding the same three records. Now copy Rovers’ confirmed record with the win’s points:
const rovers = {
team: "Rovers", played: 8, points: 13, scored: 23,
conceded: 17, form: ["W", "L", "D", "W", "L"],
};
const updated = { ...rovers, points: 16 };
console.log(updated.points, rovers.points);
console.log(updated.form === rovers.form);
The copy has the new points and the confirmed record doesn’t, but both share one form array:
16 13
true
As in a Python dict display, a later property replaces an earlier one, so points: 16 wins and the confirmed record keeps 13. Spread can also merge objects, which comes later.
Copy Maps and Sets With Their Constructors
Besides the table, Nadia’s script keeps tonight’s pitches as a Map from pitch number to home team, and the suspended players as a Set. Tom Diaz’s red card this evening is provisional, like the scores.
A Map’s or a Set’s entries aren’t properties, so object spread can’t copy them: { ...pitches }, ported from {**pitches}, gives an empty object, {}, with no error. Pass the original to the constructor instead, as you would with dict(pitches) and set(suspended) in Python. Map and Set also require new. His red card goes into the provisional suspensions only:
const pitches = new Map([[1, "Rovers"], [2, "Athletic"], [3, "Dynamo"]]);
const pitchesCopy = new Map(pitches);
console.log(pitchesCopy === pitches, pitchesCopy.get(1));
const suspended = new Set(["Joe Evans"]);
const provisionalSuspended = new Set(suspended);
provisionalSuspended.add("Tom Diaz");
console.log(suspended.size, provisionalSuspended.size);
console.log([...provisionalSuspended].join(", "));
Each copy is a separate collection, and the red card stays in the copy:
false Rovers
1 2
Joe Evans, Tom Diaz
Each of these copies is one level deep, exactly as in Python, where {**rovers}["form"] is rovers["form"] is also True. Next up is the video Keeping the Confirmed Table With structuredClone(). You’ll see what a one-level copy means for records that change, and copy every level so the confirmed table stays as it was.