Skip to content

Instantly share code, notes, and snippets.

@allenwb
Created October 5, 2012 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenwb/3837490 to your computer and use it in GitHub Desktop.
Save allenwb/3837490 to your computer and use it in GitHub Desktop.
A JSON.parse reviver from ES6 maps
function mapReviver(key, value) {
if (typeof value != "object") return value;
switch (value["<kind>"]){
case undefined: return value;
case "Map": {
let newValue = new Map;
let mapData = value["<mapData>"];
if (!mapData) return value;
mapData.forEach(e=>newValue.set(e[0], e[1]));
return newValue;
}
default: return value;
}
}
@kenyee
Copy link

kenyee commented Feb 1, 2019

Thanks much. Why this isn't just built-in, I have no idea :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment