Skip to content

Instantly share code, notes, and snippets.

@kovaldn
Created April 5, 2013 08:30
Show Gist options
  • Save kovaldn/5317585 to your computer and use it in GitHub Desktop.
Save kovaldn/5317585 to your computer and use it in GitHub Desktop.
Javascript: parse string to object (underscore)
// string: one=1&two=2&three=3
// result: Object {one: 1, two: 2, three: 3};
// see: underscorejs.org
parseQueryString: function (queryString) {
var params = {};
if(queryString){
_.each(
_.map(decodeURI(queryString).split(/&/g),function(el,i){
var aux = el.split('='), o = {};
if(aux.length >= 1){
var val = undefined;
if(aux.length == 2)
val = aux[1];
o[aux[0]] = val;
}
return o;
}),
function(o){
_.extend(params,o);
}
);
}
return params;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment