Created
August 21, 2015 23:34
-
-
Save alexdiliberto/dba15c923eababe8703e to your computer and use it in GitHub Desktop.
Converting a string Map to and from an object
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
// 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