Skip to content

Instantly share code, notes, and snippets.

@sammoore
Created January 24, 2017 19:58
Show Gist options
  • Save sammoore/eecba9449bf21925df17fa6c495436c5 to your computer and use it in GitHub Desktop.
Save sammoore/eecba9449bf21925df17fa6c495436c5 to your computer and use it in GitHub Desktop.
Converts a query string into a key-value array/object. Does not support nested objects (e.g. `?foo[bar]=foobar` becomes `{ 'foo[bar]': 'foobar' }`).
function qsShallowObject(qs) {
var string = qs.indexOf('?') == 0 ? qs.slice(1) : qs.slice();
return (string.split('&')
.map(function (qsEl) {
return qsEl.split('=').map(decodeURIComponent);
})
.reduce(function (query, keyValue) {
query[keyValue[0]] = keyValue[1];
return query;
}, {})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment