Building Objects and Sets From Arrays

Your port can now turn every finish time into seconds with .map(). The Python script at the timing desk does more than lists, though. It looks up a finish time by bib number in a dict, collects the age groups in a set, hands the results page a list of small dicts, and counts every checkpoint read. Each is one comprehension.

JavaScript has no comprehensions of any kind. In this lesson, you’ll port all four the way a JavaScript developer does, by mapping the records to the values and collections you need.

Turn Pairs Into an Object

To keep the examples short, the seven cleaned records sit in runners.js in your race-results folder. They’re the same records the Python script holds in its runners and finishers lists. The file exports runners, in the timing export’s order, and finishers, the six records 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 below 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.

When someone at the desk reads out a bib number, the Python script finds that runner’s finish time in a dict built by a dict comprehension:

Language: Python
times = {r["bib"]: r["finish"] for r in finishers}

JavaScript can’t build an object from a loop expression, but it can build one from an array of [key, value] pairs. Each pair is a two-item array, since JavaScript has no tuples. Object.fromEntries() turns those pairs into an object, as Python’s dict() does with a list of pairs, so the port maps each finisher to a pair first:

Language: JavaScript
import { finishers } from "./runners.js";

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

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.