Created
February 7, 2012 03:21
-
-
Save mattsah/1756935 to your computer and use it in GitHub Desktop.
Convert complex input names/values to javascript object
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
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