Choosing Between a Map and an Object

The booking page has to save Rosa’s updated waiting list between visits as JSON text. Her Python script keeps that list and each plot record in a dict, but your port uses a Map and an object. Here’s how to choose and how to get a Map through JSON.

Pick the Tool by Its Keys

JavaScript splits Python’s dict into two tools, so ask whether the keys are names you write or values you’re given. You type holder, so a plot record is an object. Member numbers are data, so the waiting list is a Map. Here’s how both compare with a dict:

Python dict Object (a record) Map (a lookup)
Key types Any hashable, kept Strings (or symbols): 214 becomes "214" Any value, kept
Key order Insertion Array-index keys ("0" to "4294967294") first, then insertion Insertion
Size len(d) Object.keys(plot).length (its own string keys) waiting.size
Inherited keys None "constructor" in {} is true None
Into JSON json.dumps(d) for str, int, float, bool, or None keys, as strings JSON.stringify(plot) Convert first

The inherited keys are the surprise. Python’s in checks a dict’s keys and never its methods, but an object’s keys are its properties, and every plain object inherits a few properties from a shared parent. That parent is a later topic. For now, know that in sees its properties too. Here are the record and the lookup in use:

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.