Prox4
Prox4

Reputation: 111

How to send data to an api having multiple Nested Arrays in react

I have to send the modified data to an api which has json format as below:

{
"Customer": {
    "name": "ABC",
    "email": [email protected],
    "password": ""
},
"access": true,
"time": 2000
}

On save I want to set the respective state to api fields.

save=()=>{
    let newCustomer={
        access:this.state.access,
        time:this.state.time,
        name:      //How can i set the state values for name,email and 
                     password which is in nested form?
        email:
        password:

    }
     return axios.put('api',newCustomer)
     .then(response => {

    })
}

Upvotes: 0

Views: 504

Answers (2)

Malkhazi Dartsmelidze
Malkhazi Dartsmelidze

Reputation: 4992

        save=(Customer)=>{
            let newCustomer={
                ...Customer,
                access: this.state.access,
                time: this.state.time,
            }
            return axios.put('api', newCustomer)
            .then(response => {
                console.log(response);
            })
        }

Then newCustomer will be like Customer but access and time may be different. In backend you cann access customer name and email like you are accessing an array

Upvotes: 0

Bukhari
Bukhari

Reputation: 502

You can directly declare it like your json format.

let newCustomer={
  access:this.state.access,
  time:this.state.time,
  Customer: {
    name: ..., // state name from your nested form
    email: ...,  // state email from your nested form
    password: ..., // state password from your nested form
  },
}

Upvotes: 1

Related Questions