Skip to content
Advertisement

Tag: dictionary

How do you JSON.stringify an ES6 Map?

I’d like to start using ES6 Map instead of JS objects but I’m being held back because I can’t figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be listed. Do I really have to write a wrapper method to serialize? Answer Both JSON.stringify and JSON.parse support a second argument.

Javascript equivalent of Python’s dict.setdefault?

In Python, for a dictionary d, sets d[‘key’] = value if ‘key’ was not in d, and otherwise leaves it as it is. Is there a clean, idiomatic way to do this on a Javascript object, or does it require an if statement? Answer It’s basically like using an if statement, but shorter: Or Update: as @bobtato noted, if the

Advertisement