Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Aaron3
Forked from can3p/gist:1787188
Last active December 16, 2015 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aaron3/5474890 to your computer and use it in GitHub Desktop.
Save Aaron3/5474890 to your computer and use it in GitHub Desktop.
Jquery parse query string built from .serialize() or .param() to object.
/**
* Converts a string that was serialized with jQuery.param back to the object.
*
* @param {String} str
*
* @return {Object}
*/
function parseParams(str) {
var obj = {}, pair;
var pairs = decodeURIComponent(str).split( "&" );
var injectParam = function(key, val) {
var firstBracket = key.indexOf('[');
if (firstBracket === -1) {
obj[key] = val;
return;
}
var prevkey = key.substring(0, firstBracket),
key = key.substr(firstBracket),
prev = obj,
newobj,
newkey;
key.replace(/\[([^\]]+)?\]/g, function(chunk, idx, pos) {
var newobj, newkey;
if (chunk.match(/\[\d*\]/)) {
newobj = prev[prevkey] || [];
newkey = idx || '[]';
} else {
newobj = prev[prevkey] || {};
newkey = idx;
}
if (prevkey === '[]') {
prev.push(newobj);
} else {
prev[prevkey] = newobj;
}
prev = newobj;
prevkey = newkey;
});
if (prevkey === '[]') {
prev.push(val);
} else {
prev[prevkey] = val;
}
}
//if user passes simple string, we return it as an response
if (pairs.length === 1) {
return pair[0];
}
for( var arg = 0; arg < pairs.length; arg++ ) {
pair = pairs[ arg ].split( "=" );
injectParam(pair[0], pair[1]);
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment