Skip to content

Instantly share code, notes, and snippets.

@AugustMiller
Created September 26, 2017 01:48
Show Gist options
  • Save AugustMiller/7d9dc2e8a90abb5c66ab2bbeb3a4b7a8 to your computer and use it in GitHub Desktop.
Save AugustMiller/7d9dc2e8a90abb5c66ab2bbeb3a4b7a8 to your computer and use it in GitHub Desktop.
Convert jQuery's `serializeArray` output into an object that better matches the structure of an HTTP request or POST body.
module.exports = (serializeArrayResult) ->
obj = {}
arr = serializeArrayResult
for pair in arr
if obj[pair.name] isnt undefined
if !Array.isArray obj[pair.name]
# Convert existing single value to an array:
obj[pair.name] = [obj[pair.name]]
obj[pair.name].push(pair.value or '')
else
obj[pair.name] = pair.value or ''
obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment