Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Nested objects and files

const buildFormData = (formData: FormData, data: FormVal, parentKey?: string) => {
    if (Array.isArray(data)) {
        data.forEach((el) => {
            buildFormData(formData, el, parentKey)
        })

    } else if (typeof data === "object" && !(data instanceof File)) {
        Object.keys(data).forEach((key) => {
            buildFormData(formData, (data as FormDataNest)[key], parentKey ? `${parentKey}.${key}` : key)
        })

    } else {
        if (isNil(data)) {
            return
        }

        let value = typeof data === "boolean" || typeof data === "number" ? data.toString() : data
        formData.append(parentKey as string, value)
    }
}

export const getFormData = (data: Record<string, FormDataNest>) => {
    const formData = new FormData()

    buildFormData(formData, data)

    return formData
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Merge Arrarys of Object of Any size 
Javascript :: Create Compond from route to js function communication 
Javascript :: load json object from file frontend javascript 
Javascript :: vue2-editor save image 
Javascript :: ** in javascript 
Javascript :: mysql timestamp to time/days ago function 
Javascript :: Callback after forEach completed 
Javascript :: how-to-show-a-confirm-message-before-delete 
Javascript :: change active menu item on page scroll javascript 
Javascript :: Timestamp Format: 23.12.2015 08:34:50 
Javascript :: object empty or undefined 
Javascript :: reactjs .map is not a function 
Javascript :: merge two array with same length as object 
Javascript :: sqlite get row id after insert nodejs 
Javascript :: how to refrence schema in my mongoose schema with populate function 
Javascript :: mongo look for substring of field 
Javascript :: scriptcase javascript close modal form 
Javascript :: .catch() in promise will aslo return a promise 
Javascript :: loopback 4 pagination 
Javascript :: Preselecting options select2 angularjs 
Javascript :: creating stripe token from javscript lib 
Javascript :: validate vpa api razorpay stackoverflow 
Javascript :: SH1 in react native 
Javascript :: bind() method #1 
Javascript :: pause media stream javascript 
Javascript :: Check If Key Exists For Object 
Javascript :: sum properties some objects when merge their 
Javascript :: react axios request data objest from online json with table element 
Javascript :: asp.net updatepanel autoscroll fix 
Javascript :: convert array to conventional array js 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =