You can use a reviver with JSON.parse to actually create Map instances instead of plain objects when parsing JSON so that you can use Map.prototype.has instead of Object.prototype.hasOwnProperty:
reviver
JSON.parse
Map
Map.prototype.has
Object.prototype.hasOwnProperty
JSON.parse('{"a":null,"b":{"c":[0,1]}}', (_, value) => value !== null && typeof value === "object" && !Array.isArray(value) ? new Map(Object.entries(value)) : value ); // ▸ Map(2) {"a" => null, "b" => Map(1)}
You can then also use a replacer with JSON.stringify to stringify Map instances as plain objects:
replacer
JSON.stringify
JSON.stringify( new Map([ ["a", null], ["b", new Map([["c", [0, 1]]])] ]), (_, value) => (value instanceof Map ? Object.fromEntries(value) : value) ); // "{"a":null,"b":{"c":[0,1]}}"
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can use a
reviver
withJSON.parse
to actually createMap
instances instead of plain objects when parsing JSON so that you can useMap.prototype.has
instead ofObject.prototype.hasOwnProperty
:You can then also use a
replacer
withJSON.stringify
to stringifyMap
instances as plain objects: