I am trying to have an optional field on object. If the state
variable equals active
, I want to have data.fields
equal to show
. If state
does not equal active
, I don't want terms
to even be part of the data
object. The way I have below is my workaround but null
still sends which I don't want.
let state = 'active';
let data = {
name: "John",
age: 22,
terms: state === 'active' ? 'show' : null
}
axios.post(url, data);
Is a proper alternative this:
let state = 'active';
let data = {
name: "John",
age: 22,
}
if (this.state === 'active') {
data.terms = 'show';
}
axios.post(url, data);