Skip to content

Instantly share code, notes, and snippets.

@wgottschalk
Created January 16, 2016 23:20
Show Gist options
  • Save wgottschalk/b6d67182f86015628264 to your computer and use it in GitHub Desktop.
Save wgottschalk/b6d67182f86015628264 to your computer and use it in GitHub Desktop.
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