2
async onSubmitConditionDuration() {
    var param = (this.state.health_condition+"_personal_age")
    try {
        axios.patch(URL, 
        {
          param: this.state.condition_duration,   //param is a variable that represents a field name at the endpoint(URL)

        })
       .then((response)=>{
           console.log(response.status);
        })
       .catch((errors) => {
          console.log(errors);
        })
       }
       catch(errors){
         console.log(errors);
       }
    }

I'm trying to have field names as variable in the json body of my request so that fields can be dynamically updated based on variables' values. By using the above syntax ,the patch request fails to update the field represented by the value stored in 'param'.

How can this be done in Javascript?

I'm working in a react native app

Surendhar E
  • 271
  • 1
  • 2
  • 6
  • react native working stable ? – Arunkumar Jul 21 '17 at 07:24
  • Possible duplicate of [How do I create a dynamic key to be added to a JavaScript object variable](https://stackoverflow.com/questions/2462800/how-do-i-create-a-dynamic-key-to-be-added-to-a-javascript-object-variable) – Kerumen Jul 21 '17 at 07:41

1 Answers1

1

You can use the [] syntax:

var param = (this.state.health_condition+"_personal_age")

var object = {
  [param]: this.state.condition_duration,
}
Kerumen
  • 4,193
  • 2
  • 19
  • 33