Skip to content

Instantly share code, notes, and snippets.

@nathanclevenger
Created August 30, 2022 13:57
Show Gist options
  • Save nathanclevenger/a560851a01c55a21ac0631480b169a05 to your computer and use it in GitHub Desktop.
Save nathanclevenger/a560851a01c55a21ac0631480b169a05 to your computer and use it in GitHub Desktop.
Map Stringify
// function replacer(key, value) {
// if(value instanceof Map) {
// return {
// dataType: 'Map',
// value: Array.from(value.entries()), // or with spread: value: [...value]
// };
// } else {
// return value;
// }
// }
const replacer = (key, value) => value instanceof Map ? { dataType: 'Map', value: [...value] } : value
// function reviver(key, value) {
// if(typeof value === 'object' && value !== null) {
// if (value.dataType === 'Map') {
// return new Map(value.value)
// }
// }
// return value
// }
const originalValue = [
new Map([['a', {
b: {
c: new Map([['d', 'text']])
}
}]])
]
const str = JSON.stringify(originalValue, replacer)
const newValue = JSON.parse(str, reviver)
console.log(originalValue, newValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment