Skip to content

Instantly share code, notes, and snippets.

@everdimension
Created December 3, 2015 14:59
Show Gist options
  • Save everdimension/89b1a49cc8d0b24e5aa2 to your computer and use it in GitHub Desktop.
Save everdimension/89b1a49cc8d0b24e5aa2 to your computer and use it in GitHub Desktop.
A function to parse search string params into javascript key-value object.
// sample data for trying
var paramsString = "?lunch=sandwich&dinner=stirfry";
console.log(parseParams(paramsString));
//
// main function
//
function parseParams(str) {
return str
.replace(/(^\?)/, '')
.split('&')
.reduce(function(obj, currentPair) {
var pair = currentPair.split('=');
obj[pair[0]] = pair[1];
return obj;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment