5.8. Object entries into a Map and back
Topics
This question is in
5. Objects and property controlObjects 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
function objectToMap(object) // returns a new Map
function mapToObject(map) // returns a new plain objectobjectToMap: one entry per own enumerable string key ofobject(the keysObject.keyslists), in that order.mapToObject: one property per entry ofmap. Symbol keys stay symbols; every other key becomes a string.
Requirements
objectToMapreturns a new Map with one entry per own enumerable string key, in the same order asObject.keys. Keys stay strings ({ 1: "one" }gives the key"1"), and values are the same values, not copies.- Inherited properties, non-enumerable properties and symbol keys are left out. An empty object gives an empty Map.
- Changing the Map afterwards does not change the object.
objectToMap(null)andobjectToMap(undefined)throw aTypeError. mapToObjectreturns a new plain object (its prototype isObject.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. Sonew Map([["2", "b"], ["1", "a"]])gives an object whose keys are["1", "2"].- Keys are converted to property keys: symbols stay symbols, anything else becomes a string (
1gives"1",truegives"true",nullgives"null", an object gives"[object Object]"). - When two keys become the same property key (
"1"and1), the later entry wins. - A key
"__proto__"becomes an ordinary own property; the result's prototype staysObject.prototype. - 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 byobjectToMap(rule 2), so they are not in the result.
Examples
const prices = objectToMap({ tea: 40, cake: 120 });
prices.get("cake"); // 120
prices.set("coffee", 60);
mapToObject(prices); // { tea: 40, cake: 120, coffee: 60 }mapToObject(new Map([[1, "one"], [true, "yes"]])); // { 1: "one", true: "yes" }
mapToObject(new Map([["1", "string"], [1, "number"]])); // { 1: "number" }: the later entry winsconst unsafe = mapToObject(new Map([["__proto__", { admin: true }]]));
unsafe.admin; // undefined: "__proto__" is an own key, not the prototype
objectToMap(null); // TypeErrorEnvironment
- Plain JavaScript in a browser page; the tests call the globals
objectToMapandmapToObjectfromobject-map.js. - Not allowed:
Object.entriesandObject.fromEntries. The tests delete them while your functions run.Object.keys,new Map()andfor…ofare allowed. - Nested objects and Maps are out of scope: only the top level is converted.
Hints
- What does
object["__proto__"] = valuedo on a plain object, and which function creates a property without that effect? 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