Building Objects and Sets From Arrays

Your port turns finish times into seconds with .map(). The Python script at the timing desk also builds a dict of finish times by bib, a set of age groups, a list of small dicts for the results page, and a flat list of every checkpoint read. Each collection comes from a comprehension, and JavaScript has none.

The JavaScript way is to build the array first with .map(), then turn it into the collection you need. The fourth port hides a trap.

Map the Array, Then Wrap It

The examples import the seven cleaned records from runners.js in your race-results folder. They’re the same records the Python script stores in its runners list, and finishers holds the six without a "DNF":

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

export const finishers = runners.filter(
  (r) => r.finish !== "DNF",
);

Each example starts with an import line, much like Python’s from runners import finishers, but a JavaScript module shares only the names it marks with export.

Here are the first three comprehensions next to their ports:

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

Become a Member to join the conversation.