nested objects and files

Solutions on MaxInterview for nested objects and files by the best coders in the world

showing results for - "nested objects and files"
Helene
17 Jan 2017
1const buildFormData = (formData: FormData, data: FormVal, parentKey?: string) => {
2    if (Array.isArray(data)) {
3        data.forEach((el) => {
4            buildFormData(formData, el, parentKey)
5        })
6
7    } else if (typeof data === "object" && !(data instanceof File)) {
8        Object.keys(data).forEach((key) => {
9            buildFormData(formData, (data as FormDataNest)[key], parentKey ? `${parentKey}.${key}` : key)
10        })
11
12    } else {
13        if (isNil(data)) {
14            return
15        }
16
17        let value = typeof data === "boolean" || typeof data === "number" ? data.toString() : data
18        formData.append(parentKey as string, value)
19    }
20}
21
22export const getFormData = (data: Record<string, FormDataNest>) => {
23    const formData = new FormData()
24
25    buildFormData(formData, data)
26
27    return formData
28}
29
similar questions
queries leading to this page
nested objects and files