Last active
December 14, 2017 23:21
-
-
Save bUxEE/1294c40a7b2bcb7823af87d76f52e181 to your computer and use it in GitHub Desktop.
grouped inputs to json
This file contains 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
var group_fields = []; | |
// Loop through the fields | |
theForm.serializeArray().forEach(function(entry,i) { | |
// Get the index and prop name from the entry name | |
var nameparts = /^([a-zA-Z0-9_]+)\[(.+)\]\[(.*)\]$/.exec(entry.name); | |
// Get the group entry if we already have it | |
var group = group_fields[nameparts[2]]; | |
if (!group) { | |
// We don't, create and add it | |
group = group_fields[nameparts[2]] = {}; | |
} | |
// Set the property (address, street, etc.) | |
group_fields[nameparts[2]][nameparts[3]] = entry.value; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment