Created
August 30, 2022 13:57
-
-
Save nathanclevenger/a560851a01c55a21ac0631480b169a05 to your computer and use it in GitHub Desktop.
Map Stringify
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
// 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