Skip to content

Instantly share code, notes, and snippets.

@jeremy-code
Created March 6, 2024 03:06
Show Gist options
  • Save jeremy-code/4c09ff20db149d6e653640d4fcd706ab to your computer and use it in GitHub Desktop.
Save jeremy-code/4c09ff20db149d6e653640d4fcd706ab to your computer and use it in GitHub Desktop.
parseFormData.ts
export const parseFormData = (formData: FormData) =>
Array.from(formData.entries()).reduce(
(acc, [k, v]) => {
if (!acc[k]) {
const values = formData.getAll(k);
acc[k] = values.length > 1 ? values : v;
}
return acc;
},
{} as Record<string, FormDataEntryValue | FormDataEntryValue[]>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment