Skip to content

Instantly share code, notes, and snippets.

@alexdiliberto
Created August 21, 2015 23:34
Show Gist options
  • Save alexdiliberto/dba15c923eababe8703e to your computer and use it in GitHub Desktop.
Save alexdiliberto/dba15c923eababe8703e to your computer and use it in GitHub Desktop.
Converting a string Map to and from an object
// The following two function convert string Maps to and from objects:
// http://www.2ality.com/2015/08/es6-map-json.html
function strMapToObj(strMap) {
let obj = Object.create(null);
for (let [k,v] of strMap) {
// We don’t escape the key '__proto__'
// which can cause problems on older engines
obj[k] = v;
}
return obj;
}
function objToStrMap(obj) {
let strMap = new Map();
for (let k of Object.keys(obj)) {
strMap.set(k, obj[k]);
}
return strMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment