Keeping the Confirmed Table With structuredClone()
Week 9’s results stay provisional until the league committee confirms them, so Nadia’s Python script applies them to a deep copy of the confirmed table. In this lesson, you ported that copy to JavaScript and saw why spread isn’t enough for it.
Here’s the step in the Python script, with comment rows standing in for the rest of the file:
import copy
# POINTS, table, RESULTS and the helpers
provisional = copy.deepcopy(table)
for home, home_goals, away, away_goals in RESULTS:
home_team = find_team(provisional, home)
away_team = find_team(provisional, away)
record(home_team, home_goals, away_goals)
record(away_team, away_goals, home_goals)
# print the table, each move and the leaders
- Python prints the provisional table with
up 1beside Rovers and Dynamo,down 1beside Athletic and Wanderers, andConfirmed leaders: Athletic. Each move compares a team’s new place with its place in the confirmed table. - JavaScript has no
copymodule, so a quick port reaches for the copy you already know,const provisional = [...table];. The loop carries over as afor...ofloop, and since JavaScript has no tuples, each result is a record withhome,homeGoals,awayandawayGoals. - The port runs without an error, but every move comes out
same, and the last line saysConfirmed leaders: Rovers, before the committee has confirmed a single result. - Spread built a new outer array of its own. But its six slots point at the same six records as the confirmed table.
record()changes a record in place, as it does in Python, so recording Rovers’ win in the copy gave the confirmed Rovers 9 played and 16 points too. Both tables hold the same records, so every team’s confirmed place matches its new one, and every move issame. - Python’s
table.copy()does exactly the same: spread is a shallow copy, not a deep one. - A loop that pushes a spread copy of each record,
provisional.push({ ...team }), gets the moves right. But each copy’s form array is still the confirmed record’s own, as it is with Python’s[dict(team) for team in table], so the confirmed Rovers form changes from W L D W L to L D W L W.
Here’s the port with the copy that goes all the way down:
// POINTS, table, RESULTS and the helpers
const provisional = structuredClone(table);
for (const match of RESULTS) {
const home = findTeam(provisional, match.home);
const away = findTeam(provisional, match.away);
record(home, match.homeGoals, match.awayGoals);
record(away, match.awayGoals, match.homeGoals);
}
// print the table, each move and the leaders
structuredClone() is a global function, so there’s nothing to import. It copies the array, every record inside it and every form array inside those, so the copy shares nothing with the confirmed table, like Python’s copy.deepcopy(). The port now prints Python’s lines:
Week 9 table (provisional)
1. Rovers 16 pts, form LDWLW, up 1
2. Athletic 16 pts, form WDWWD, down 1
3. Dynamo 13 pts, form LWDLW, up 1
4. Wanderers 13 pts, form DLWWD, down 1
5. United 9 pts, form WLLWL, same
6. Albion 7 pts, form LDWLL, same
Confirmed leaders: Athletic
structuredClone() copies Dates, Maps and Sets too. An older deep copy, still common in code you’ll find online, goes through JSON, and it turns the season’s kickoff Date into a string:
const season = {
name: "Autumn 2026",
nextKickoff: new Date("2026-11-12T19:30"),
};
const seasonCopy = JSON.parse(JSON.stringify(season));
console.log(seasonCopy.nextKickoff instanceof Date);
console.log(structuredClone(season).nextKickoff instanceof Date);
The JSON copy’s kickoff is no longer a Date, and the clone’s still is:
false
true
Python’s json.dumps(season) refuses the datetime instead, with TypeError: Object of type datetime is not JSON serializable.
Watch out for a record holding a function, such as a tie-break rule: structuredClone() throws DOMException [DataCloneError]: (a, b) => b.scored - a.scored could not be cloned., where Python’s copy.deepcopy() shares the function. (DOMException is the web-standard error type.) Keep functions out of the data you clone: put a function next to the data, not inside it.
The rule to keep: ask how deep a copy must go. When the code only adds, removes or reorders items, a spread copy is enough. When it changes the records themselves, call structuredClone().
Next up is the exercise Draft the Provisional Scorers and Suspensions, where you’ll put this week’s goals and a red card into provisional copies of the scorers’ tally and the suspensions, while the confirmed ones stay as they were.