Skip to content

Instantly share code, notes, and snippets.

@ryochin
Created July 16, 2022 01:39
Show Gist options
  • Select an option

  • Save ryochin/2f617d9d797d2f87f35db72d287ea4d7 to your computer and use it in GitHub Desktop.

Select an option

Save ryochin/2f617d9d797d2f87f35db72d287ea4d7 to your computer and use it in GitHub Desktop.
TypeScript version of jQuery's serializeArray()
// see also: https://barker.codes/blog/serialize-form-data-into-an-array-in-vanilla-js/
// NOTE: type FormDataEntryValue = File | string
type SerializeArray = { [x: string]: FormDataEntryValue }
const serializeArray = (form: HTMLFormElement): SerializeArray => {
const result: SerializeArray = <SerializeArray>{}
const formData: FormData = new FormData(form)
formData.forEach((val: FormDataEntryValue, key: string) => result[key] = val)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment