Skip to content

Instantly share code, notes, and snippets.

@bUxEE
Last active December 14, 2017 23:21
Show Gist options
  • Save bUxEE/1294c40a7b2bcb7823af87d76f52e181 to your computer and use it in GitHub Desktop.
Save bUxEE/1294c40a7b2bcb7823af87d76f52e181 to your computer and use it in GitHub Desktop.
grouped inputs to json
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