Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
JavaScript - Convert a query string to an object literal.
function queryString2Obj(str) {
var ampArr = str.replace(/(^\?)/, '').split('&');
function decode(estr) {
return decodeURIComponent(estr).replace(/\+/g, ' ');
}
function mapHandler(cv) {
cv = cv.split('=');
this[cv[0]] = decode(cv[1]);
return this;
}
return ampArr.map(mapHandler, {})[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment