From Dicts to JavaScript Objects for Pythonistas (Summary)

Marta’s label maker now runs in JavaScript. You ported the Python script, then finished order 1043’s label in Finish the Shipping Label. The label includes the remote-area surcharge, bringing postage to £7.70, and keeps the £18.50 price because the order isn’t a gift. The courier gets indented JSON with the address, phone, cover, parcels, and cleaned tags.

You can now build and inspect objects, look up postage, supply defaults, deduplicate tags, and read and write JSON. Keep these choices handy when porting your next dict-based script:

  • Read values with dots for known keys, such as order.id, and brackets for variable keys, such as RATES[country]. In an object literal, [HOME_COUNTRY]: 3.2 uses the variable’s value as the key.
  • List keys, values, or pairs with Object.keys(), Object.values(), or Object.entries(). Numeric object keys become strings. Choose a Map when numeric keys must stay numbers.
  • Deduplicate tags with new Set(), check membership with .has(), and use Object.hasOwn() to check an object’s own keys. JavaScript’s in checks properties, including inherited ones.
  • Parse order text with JSON.parse(). Before JSON.stringify(label, null, 2), turn Sets into arrays with [...tags] so their members reach the label.

These choices keep the data’s meaning intact, from the postage lookup to the courier’s booking form.

Decide What Missing Data Means

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.