Reading an Order as an Object

Marta’s label maker needs to read order 1042 for Iris Okafor in Leeds before it can choose an address or postage. Her Python script holds the order in a dict. You’ll port its fields to a JavaScript object, then start building the courier’s label.

Write the Order as an Object

A JavaScript object literal uses braces like a Python dict. Each key-value pair is a property. This excerpt keeps only the fields needed here, with null replacing Python’s None for the deliberately empty shipping address:

Language: JavaScript
const order = {
  id: 1042,
  billing_address: {
    city: "Leeds",
    postcode: "LS6 2QA",
  },
  shipping_address: null,
};

const declares the variable and prevents reassignment. The keys keep the platform’s snake_case spelling, while your own variables use camelCase.

Unlike Python, JavaScript reads a bare key as a literal name: id: 1042 means "id": 1042, without looking up a variable. Quotes are optional for identifier names such as id, but necessary for a key containing a space, such as "gift message". Use bare names for the platform’s ordinary fields.

Read the Order’s Fields

For the known string field names in this order record, a plain object serves the role of Marta’s Python dict. It supports bracket access and dot access, but Python’s dict has only the former. For fixed field names, a dot names the property directly, and nested access follows the same pattern:

Language: JavaScript
const order = {
  id: 1042,
  billing_address: { city: "Leeds", postcode: "LS6 2QA" },
  shipping_address: null,
};
console.log(order.id);
console.log(order["id"]);
console.log(order.billing_address.city);

Both access forms read the ID, and the chain reads the city:

Language: Program Output
1042
1042
Leeds

This chain works because billing_address is an object. Accessing a property of null or undefined throws TypeError: order.shipping_address.city would fail with the first excerpt’s empty address.

The syntax choices for the label maker are:

Python JavaScript
order["id"] order.id or order["id"]
order["gift message"] order["gift message"]
order["billing_address"]["postcode"] order.billing_address.postcode

Use dots for fixed names and brackets with a string for keys a dot can’t express. Watch out: order.phone or a misspelled order["Id"] gives undefined instead of Python’s KeyError, so check spelling first. In these examples, the fields weren’t supplied. An existing property can also hold undefined, so its value alone doesn’t establish whether a field exists. Here null deliberately marks the empty shipping address.

Build the Label’s Properties

Now that you can read the order, copy its ID into the label with order.id. Marta’s script temporarily records a service, but the courier picks that from its own menu and rejects the extra field, so remove it before printing:

Language: JavaScript
const order = {
  id: 1042,
  billing_address: { city: "Leeds", postcode: "LS6 2QA" },
  shipping_address: null,
};
const label = { order: order.id };
label.service = "standard";
console.log(label);
delete label.service;
console.log(label);

The assignment adds the property, and delete removes it:

Language: Program Output
{ order: 1042, service: 'standard' }
{ order: 1042 }

Adding or replacing a value works like Python’s label["service"] = "standard". An object assigned to a const variable can still change because the variable keeps pointing at that object. delete label.service corresponds to del label["service"], but a missing key causes no error in JavaScript. Unlike deleting an array element, removing this object’s property leaves no empty slot.

Use bare keys, dots for known fields, and bracketed strings when a name needs quotes. Next, in Looking Up Postage by Country, you’ll use the country from the order to look up Marta’s postage rate.

Become a Member to join the conversation.