Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Spreading Arrays and Objects

Spreading Arrays and Objects

In this lesson, you ported the lines of the race results script that join, print and copy with * and **. In JavaScript, three dots do the work of both.

  • In square brackets, three dots spread an iterable’s items, as Python’s * does in a list: [*wave_one, *wave_two] becomes [...waveOne, ...waveTwo], one array of all seven runners. It’s the same spread as push(...waveTwo), [...new Set(...)] and [..."F35"], which gives [ 'F', '3', '5' ].
  • In a call, they pass each item as its own argument: print("Missing chip reads:", *missing) becomes console.log("Missing chip reads:", ...missing), and both print Missing chip reads: 23 58. Without the dots, both print the list itself.
  • In braces, three dots copy an object’s properties, as ** does in a dict: {**runner, "checkpoints": […]} becomes { ...runner, checkpoints: […] }. The later key wins, and runner itself keeps its gap. As in Python, the copy is shallow: { ...runner } shares runner’s checkpoints array. Replacing it is safe, but pushing a read onto it changes runner too.
  • A plain object isn’t iterable, so [...times], the port of Python’s [*times], stops with TypeError: times is not iterable. A plain object spreads only into another object, such as { ...times }.
  • Ask for the one you mean: Object.keys(times) for the keys, which come back as strings (fine for listing bibs), Object.values(times) for the values, or Object.entries(times) for the [key, value] pairs. A Map, such as timesMap = new Map(finishers.map((r) => [r.bib, r.finish])), keeps number keys, but [...timesMap] spreads into pairs, like Python’s [*times.items()], not into keys like [*times].
  • Spreading numbers into Math.min() comes later in the course.

Here’s the port, with the two waves’ timing lines written out and Python’s [*times] asked for by name:

Language: JavaScript
const WAVE_ONE = [
  "7,Ana Ruiz,F35,0:24:10,0:48:35,1:13:02,1:42:37",
  "12,Tom Okafor,M40,0:21:05,0:42:30,1:04:12,1:30:48",
  "23, Mei Chen,F35,0:22:40,,1:08:55,1:36:20",
  "31,Lars Berg,M40,0:23:15,0:47:01,,DNF",
];
const WAVE_TWO = [
  "44,Priya Nair,F20,0:20:30,0:41:10,1:02:05,1:27:44",
  "58,Sam Kowalski ,M40,0:22:50,0:45:12,,1:35:03",
  "66,Jo Lindqvist,F20,0:23:30,0:47:55,1:12:40,1:41:15",
];

function toRecord(line) {
  const [bib, name, group, ...splits] = line
    .split(",")
    .map((c) => c.trim());
  return {
    bib: Number(bib),
    name,
    group,
    checkpoints: splits.slice(0, -1),
    finish: splits.at(-1),
  };
}

const waveOne = WAVE_ONE.map(toRecord);
const waveTwo = WAVE_TWO.map(toRecord);

const runners = [...waveOne, ...waveTwo];
const finishers = runners.filter((r) => r.finish !== "DNF");
const missing = finishers
  .filter((r) => r.checkpoints.includes(""))
  .map((r) => r.bib);
console.log("Missing chip reads:", ...missing);

const runner = runners[2];
const fixed = {
  ...runner,
  checkpoints: ["0:22:40", "0:45:58", "1:08:55"],
};

const times = Object.fromEntries(
  finishers.map((r) => [r.bib, r.finish]),
);
const bibs = Object.keys(times);
console.log(bibs);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The timing company sends the chip reads in two files, one per start wave, and the live results page is moving to JavaScript. You’ll port the Python lines that join, print and copy with a star or a double star.

00:14 The script joins the two waves into one list. It prints the bibs with a missing chip read, copies Mei Chen’s record with a correction, and lists the finishers’ bibs.

00:26 In Python, a star inside square brackets unpacks each wave’s records into the new list. The JavaScript port writes three dots where Python writes the star.

00:38 The result is one array of all seven runners, in export order. You’ve met these dots before. They add wave two’s runners with push(), and they turn a Set into an array.

00:51 It’s one idea: in square brackets or a call, three dots spread out the items of an iterable. A string’s characters spread too, as with Python’s star.

01:04 Bibs 23 and 58 are finishers who each lack a checkpoint read. Python’s print() gets a star in front of missing, so each bib arrives as its own argument.

01:16 In the port, the same three dots go in front of missing, inside the console.log() call. Run both. Each prints the label and the two bibs, with spaces between them.

01:29 Without the dots, both would print the list itself, brackets and all.

01:35 Mei Chen’s 10 km read was missing, but a backup mat logged her there. Python’s double star copies every key of her record, and a new checkpoints list follows.

01:46 In JavaScript, three dots inside braces do the double star’s work. They copy the record’s properties. Then checkpoints comes again, and the later key wins, as in Python.

02:00 Mei’s own record keeps its gap: the correction is a new object. As in Python, the copy is shallow: a plain copy shares her checkpoints array. Replacing that array is safe.

02:14 Pushing a read onto it would change Mei’s original too.

02:19 Last, the results page needs the finishers’ bibs. Python’s times maps each bib to a finish time, and a star on a dict unpacks its keys. Python prints the six bibs.

02:33 In the port, times is the object that Object.fromEntries() builds. The star becomes three dots, as everywhere else in this file.

02:43 What does the port print? Your turn: pause the video and predict.

02:53 The first line matches. Then JavaScript stops with a type error: times is not iterable.

03:03 In brackets and calls, three dots need an iterable. A Python dict is one: stepping through it gives its keys. In JavaScript, arrays, strings, Sets and Maps are iterable.

03:17 A plain object isn’t: it’s a record of named fields, not a collection, so it doesn’t choose keys, values or pairs. Inside braces, the dots copy properties, so it spreads into another object.

03:32 So ask for the one you mean. Object.keys() gives the bibs. They’re strings: an object stores number keys as strings. For listing bibs, that’s fine.

03:45 Object.values() gives the finish times, and Object.entries() the pairs. A Map keeps the bibs as numbers, but spreads into pairs, like Python’s items(), not keys.

03:59 Three dots do the work of both the star and the double star. In brackets or a call, they spread an iterable’s items. In braces, they copy an object’s properties.

04:12 A plain object spreads only into another object, so ask for its keys, values or pairs with Object methods. Spreading numbers into Math.min() comes later in the course.

04:25 Next up is the text lesson Destructuring Objects by Name. There, you’ll take a record’s fields by name in one line, where Python usually reads them one lookup at a time.

Become a Member to join the conversation.