Created
October 30, 2012 15:38
-
-
Save gregersrygg/3981010 to your computer and use it in GitHub Desktop.
Array reduce example to convert a query-string to parameter object (requires EcmaScript 5)
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
var queryString = "foo=bar&a=b%C3%B8"; | |
var decode = decodeURIComponent; | |
var paramObj = queryString.split("&").reduce(function (obj, pair) { | |
var keyVal = pair.split("="); | |
var key = keyVal[0]; | |
var val = keyVal[1]; | |
obj[ decode(key) ] = decode(val); | |
return obj; | |
}, {}); // initial value for obj | |
// paramObj == {foo: "bar", a: "bø"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment