1

I am trying to dynamically add the field name and value in the javascript object using es6 structuring. The problem is its not using the value just the fieldName

updateValue(event){

        let varName = event.target.dataset.field;

        this.realFormData = {...this.realFormData , varName : event.detail.value};
        console.log( this.realFormData); 
    }

Console.log OP: {somefield:"someValue" , varname : "somevalue"}

As you can see, it has used field as varName and not the value of varName

How to bind value of varName instead of its string?

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
susanoo chidori
  • 206
  • 6
  • 19
  • Wrap your object's key with third bracket, like this {[varname]: "somevalue""} – Robin Apr 09 '19 at 09:48
  • Try using [Object.defineProperty](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) – Kunal Mukherjee Apr 09 '19 at 09:53

1 Answers1

4

Like this :

this.realFormData = {...this.realFormData , [varName] : event.detail.value};
ostrebler
  • 940
  • 9
  • 32