Skip to content

Instantly share code, notes, and snippets.

@mattsah
Created February 7, 2012 03:21
Show Gist options
  • Save mattsah/1756935 to your computer and use it in GitHub Desktop.
Save mattsah/1756935 to your computer and use it in GitHub Desktop.
Convert complex input names/values to javascript object
function(inputs){
var values = {};
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].name.indexOf('[') != -1) {
var parts = inputs[i].name.split(/\[|\]\[|\]/g);
var value = inputs[i].value;
for (var j = parts.length; j > 0; j--) {
if (parts[j - 1]) {
var enclosure = {};
enclosure[parts[j - 1]] = value;
value = enclosure;
}
}
jQuery.extend(true, values, value);
} else {
values[inputs[i].name] = inputs[i].value;
}
}
return values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment