Last active
October 17, 2016 08:56
-
-
Save Aleksey-Danchin/be731287b459747d150604890aa44910 to your computer and use it in GitHub Desktop.
Делаем коллекцию Map JSON-представляемой.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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