Loading video player…

Keeping the Confirmed Table With structuredClone()

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:

Language: Python
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 1 beside Rovers and Dynamo, down 1 beside Athletic and Wanderers, and Confirmed leaders: Athletic. Each move compares a team’s new place with its place in the confirmed table.
  • JavaScript has no copy module, so a quick port reaches for the copy you already know, const provisional = [...table];. The loop carries over as a for...of loop, and since JavaScript has no tuples, each result is a record with home, homeGoals, away and awayGoals.
  • The port runs without an error, but every move comes out same, and the last line says Confirmed 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 is same.
  • 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:

Language: JavaScript
// 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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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.

00:02 In the last lesson, you copied arrays and records with spread. Now you’ll port the line of Nadia’s script that keeps the confirmed table safe. Results stay provisional until the committee confirms them, so the Python script works on a deep copy of the table.

00:19 The loop records the evening’s three results in the copy. Python then prints the provisional table. Each move compares a team’s new place with its place in the confirmed table.

00:31 Two teams move up one place, two move down one, and the confirmed leaders are still Athletic.

00:40 JavaScript has no copy module to import. So a quick port reaches for the copy you already know, spread. The loop carries over too. JavaScript has no tuples, so each result in the port is a record, with the teams and their goals.

00:58 The helpers are ported too. The record() function still changes a team’s record in place, as in Python.

01:06 Python printed up one and down one beside the teams that moved. The port runs without an error. What does it print in the moves column? Your turn: pause the video and predict.

01:24 Run it, and every move says same. The last line names Rovers as the confirmed leaders. Python says Athletic, and no result has been confirmed yet.

01:38 Spread built a new outer array. But its six slots point at the same six records as the confirmed table. The record() function updates the record itself.

01:50 So when the loop records Rovers’ win in the copy, the confirmed table changes too. Rovers there now have sixteen points. Both tables hold the same records, so every team’s confirmed place matches its new one.

02:06 Python’s table.copy() would do exactly this. Spread is a shallow copy, like that one.

02:15 What if the port copies each record too? A loop pushes a spread copy of every team into a new array. Run it, and the moves come out right.

02:27 But look at the confirmed Rovers form. It ended with a loss, and now it ends with a win. Each record copy is new, but its form array is still shared, as with Python’s copy of each dict.

02:40 So when record() pushes a result and shifts the oldest off, it changes the confirmed form too.

02:49 To copy every level, call structuredClone() with the table. It’s a global function, so there’s nothing to import. It copies the array, every record and every form array, so the copy shares nothing, like Python’s deepcopy().

03:05 Run it again, and the port prints Python’s lines. Two teams move up, two move down, and the confirmed leaders are Athletic.

03:16 structuredClone() copies Dates, Maps and Sets too. Here’s the season record, with its next kickoff as a Date. An older deep copy, still common online, goes through JSON, JSON.stringify() and then JSON.parse().

03:31 That copy’s kickoff comes back as a string. Python’s json.dumps() refuses the datetime instead. With structuredClone(), the kickoff stays a Date.

03:43 Watch out for functions, though. A record holding one, such as a tie-break rule, stops structuredClone() with a DataCloneError.

03:52 Python’s deepcopy() shares the function instead. So keep functions out of the data you clone.

04:01 The rule to keep is to 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().

04:17 Next up is the exercise Draft the Provisional Scorers and Suspensions. 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.

Become a Member to join the conversation.