Created
September 26, 2017 01:48
-
-
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.
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
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