Reputation: 4189
I have to stringify some parameter to put it in a http patch request my method is something like this
let param = JSON.stringify({
code : this.code,
name : this.name,
fieldName: this.data
});
In that case I have a param like this:
code:3,name:'asdf',fieldName: 'col'
But I want the value of fieldName ond not the word fieldName... Is there a method to expand or evaluate this parameter name?
Upvotes: 2
Views: 5139
Reputation: 193261
If you want value of code
variable to be used as a key name, then you need to use computed object property notation:
let param = JSON.stringify({
[code]: this.code,
[name]: this.name,
[fieldName]: this.data
});
In the old days we would have to create object separately and assign variable keys in separate step using bracket notation:
let params = {}
params[code] = this.code
params[name] = this.name
params[fieldName] = this.data
let param = JSON.strigify(params)
Upvotes: 5