Created
April 5, 2013 08:30
-
-
Save kovaldn/5317585 to your computer and use it in GitHub Desktop.
Javascript: parse string to object (underscore)
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
// 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