Skip to curriculum
Jump to the editor

5.8. Object entries into a Map and back

Easy
Topics
10–20 min

Objects and Maps both hold key-value pairs, and code moves data between them all the time: a Map is better for a cache that grows and shrinks, an object is what JSON.stringify and most APIs expect. The conversion looks trivial, but the two do not agree on what a key is. A Map key can be any value; an object key is only ever a string or a symbol. Converting between them means deciding what happens to 1, true, symbols, and a key called __proto__.

Write objectToMap(object) and mapToObject(map) in object-map.js, without Object.entries or Object.fromEntries.

Signature

JAVASCRIPT
function objectToMap(object) // returns a new Map
function mapToObject(map)    // returns a new plain object
  • objectToMap: one entry per own enumerable string key of object (the keys Object.keys lists), in that order.
  • mapToObject: one property per entry of map. Symbol keys stay symbols; every other key becomes a string.

Requirements

  1. objectToMap returns a new Map with one entry per own enumerable string key, in the same order as Object.keys. Keys stay strings ({ 1: "one" } gives the key "1"), and values are the same values, not copies.
  2. Inherited properties, non-enumerable properties and symbol keys are left out. An empty object gives an empty Map.
  3. Changing the Map afterwards does not change the object. objectToMap(null) and objectToMap(undefined) throw a TypeError.
  4. mapToObject returns a new plain object (its prototype is Object.prototype) with one property per entry, created in the Map's order. The object then lists its keys the way every JavaScript object does: integer-like keys ("1", "10") first in ascending order, then the other strings in the Map's order, then symbols in the Map's order. So new Map([["2", "b"], ["1", "a"]]) gives an object whose keys are ["1", "2"].
  5. Keys are converted to property keys: symbols stay symbols, anything else becomes a string (1 gives "1", true gives "true", null gives "null", an object gives "[object Object]").
  6. When two keys become the same property key ("1" and 1), the later entry wins.
  7. A key "__proto__" becomes an ordinary own property; the result's prototype stays Object.prototype.
  8. The Map is not changed. A round trip mapToObject(objectToMap(object)) gives a new object with the same own enumerable string keys, values and key order. Only that data makes the trip: symbol keys, non-enumerable and inherited properties are left out by objectToMap (rule 2), so they are not in the result.

Examples

JAVASCRIPT
const prices = objectToMap({ tea: 40, cake: 120 });
prices.get("cake");            // 120
prices.set("coffee", 60);
mapToObject(prices);           // { tea: 40, cake: 120, coffee: 60 }
JAVASCRIPT
mapToObject(new Map([[1, "one"], [true, "yes"]]));     // { 1: "one", true: "yes" }
mapToObject(new Map([["1", "string"], [1, "number"]])); // { 1: "number" }: the later entry wins
JAVASCRIPT
const unsafe = mapToObject(new Map([["__proto__", { admin: true }]]));
unsafe.admin;                  // undefined: "__proto__" is an own key, not the prototype
objectToMap(null);             // TypeError

Environment

  • Plain JavaScript in a browser page; the tests call the globals objectToMap and mapToObject from object-map.js.
  • Not allowed: Object.entries and Object.fromEntries. The tests delete them while your functions run. Object.keys, new Map() and for…of are allowed.
  • Nested objects and Maps are out of scope: only the top level is converted.

Hints

  1. What does object["__proto__"] = value do on a plain object, and which function creates a property without that effect?
  2. String(symbol) works, but is a string what you want for a symbol key?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…