Convert Object to FormData
Convert Object to FormData is a reusable JavaScript snippet. Converts a nested JavaScript object into FormData, handling files, arrays, and nested objects — useful for API file uploads. The full implementation: function objectToFormData(obj, form = new FormData(), parentKey = '') { for (const [key, value] of Object.entries(obj)) { const formKey = parentKey ? `${parentKey}[${key}]` : key if (value instanceof File || value instanceof Blob) { form.append(formKey, value) } else if (Array.isArray(value)) { value.forEach((item, i) => { const arrayKey = `${formKey}[${i}]` if (typeof item === 'object' && item !== null) { objectToFormData(item, form, arrayKey) } else { form.append(arrayKey, item) } }) } else if (typeof value === 'object' && value !== null)… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.