Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Last active October 17, 2016 08:56
Show Gist options
  • Save Aleksey-Danchin/be731287b459747d150604890aa44910 to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/be731287b459747d150604890aa44910 to your computer and use it in GitHub Desktop.
Делаем коллекцию Map JSON-представляемой.
Object.defineProperty(Map.prototype, 'toJSON', {
enumerable: false
, configurable: true
, get: () => function () {
const obj = {};
for (const key of this.keys())
obj[key] = this.get(key);
return obj;
}
});
const collection = new Map;
collection.set('hello', 'world');
collection.set('foo', 1);
collection.set(1, 'bar');
console.log(JSON.stringify(collection));
// {"1":"bar","hello":"world","foo":1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment