Skip to content

Instantly share code, notes, and snippets.

@gracefullight
Created September 15, 2017 02:47
Show Gist options
  • Save gracefullight/f62ce16663ba9becedf69c624d38cc5c to your computer and use it in GitHub Desktop.
Save gracefullight/f62ce16663ba9becedf69c624d38cc5c to your computer and use it in GitHub Desktop.
var queryToObject = function(queryString) {
var query = queryString || location.search.replace(/\?/, "");
return query.split("&").reduce(function(obj, item, i) {
if(item) {
item = item.split('=');
obj[item[0]] = item[1];
return obj;
}
}, {});
};
// test.com/?item1=1&item2=2;
queryToObject(); // { item1: 1, item2: 2 };
queryToObject('item3=3&item4=4'); // { item3: 3, item4: 4 };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment