Last active
September 27, 2023 06:56
-
-
Save RoyalIcing/b03f63532d73551b895bdada537efe31 to your computer and use it in GitHub Desktop.
Parsing JSON to Map
This file contains 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
// Hard to read nested arrays. | |
const c = new Map([['a', 1], ['b', 2]]) | |
console.log(c) | |
// Have to write using object wrapped in two layers of calls. | |
const d = new Map(Object.entries({ a: 1, b: 2})) | |
console.log(d) |
This file contains 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
let a = JSON.stringify({ a: { b: 2, c: 3 }, d: [4, 5] }) | |
let b = JSON.parse(a, (key, value) => { | |
if (typeof value === "object" && !Array.isArray(value)) { | |
return new Map(Object.entries(value)) | |
} else { | |
return value | |
} | |
}) | |
console.log(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment