Created
December 3, 2015 14:59
-
-
Save everdimension/89b1a49cc8d0b24e5aa2 to your computer and use it in GitHub Desktop.
A function to parse search string params into javascript key-value 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
// 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