Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to form the nested object in Formdata in javascript

how to append the nested object in FormData, my object is

{
    name: 'test',
    phone: '454353',
    address: [
        {
            state: 'ewrwer',
            city: 'asdasd'
        }
    ]
}

I had append like this

const data = new FormData();
data.append("name", "test");
data.append("phone", "454353");
data.append("address['state']", "ewrwer");
data.append("address['city']", "asdasd");

but this is not working for me when I send this formData in request body the address is not working.


1 Answers

It should be:

data.append("address[0]['state']", "ewrwer");
data.append("address[0]['city']", "asdasd");

Because, it is inside array of index 0, then inside that you have addresses.

like image 178
Biplove Lamichhane Avatar answered Oct 21 '25 22:10

Biplove Lamichhane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!