Created
January 16, 2016 23:20
-
-
Save wgottschalk/b6d67182f86015628264 to your computer and use it in GitHub Desktop.
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 buildObject(str) { | |
var newObj = {}; | |
var contents = str.substring(1, str.length-1).split(","); | |
if (!contents[0]) return newObj; | |
contents.forEach(function(keyValuePair) { | |
var keyAndValue = keyValuePair.split(":"); | |
var key = keyAndValue[0]; | |
var value = keyAndValue[1]; | |
newObj[key] = parse(value); // parse is a helper function | |
}); | |
return newObj; | |
} | |
buildObject('{a:1, b:"hi"}'); // returns {a:1, b:"hi"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment