Destructuring Objects by Name

Your port can take a chip row apart and join the two start waves into one array of records. Each line on the results page, though, needs only a few fields from one runner, and the Python script reads them one lookup at a time.

In this lesson, you’ll port those lookups to a single line that takes fields by name, which pays off most in callbacks and loops.

Take Fields by Name

In the Python script, the results line for one runner starts with a lookup per field:

Language: Python
name = runner["name"]
finish = runner["finish"]
print(name, finish)

For Mei Chen’s record, that prints Mei Chen 1:36:20. You may also have taken several values at once with a mapping pattern in match, as in case {"name": name, "finish": finish}:. JavaScript makes that idea a plain declaration. Here are two of Mei’s fields taken in one line:

Language: JavaScript
const runner = {
  bib: 23, name: "Mei Chen", group: "F35",
  checkpoints: ["0:22:40", "", "1:08:55"],
  finish: "1:36:20",
};

const { finish, name } = runner;
console.log(name, finish);

Each name gets the value of the property it matches:

Language: Program Output
Mei Chen 1:36:20

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.