Skip to content

Instantly share code, notes, and snippets.

@wildeyes
Last active June 24, 2016 12:43
Show Gist options
  • Save wildeyes/408820b5997c5ce65d169186d24ff12d to your computer and use it in GitHub Desktop.
Save wildeyes/408820b5997c5ce65d169186d24ff12d to your computer and use it in GitHub Desktop.
ES 6 snippet to extract and convert the query/search string from location to a JSON object
String.prototype.remove = function(str) { return this.replace(str, ''); };
String.prototype.surround = function(char, char2=char) { return char + this + char2; };
// String.prototype.print = function() { console.log(this); return this; };
foo = location.search
.remove('?')
.split('&')
.map(exp => exp.split('=').map(str => str.surround('"')).join(':').surround('{', '}'))
// .print()
.join(',')
.surround('[', ']')
JSON.parse(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment