I have some code within a function that looks like this:
const updatedItemsArr = [...this.state.targetItems, {relationship: value}];
What I want to do is pass in a dynamic value in place of relationship. However, whatever I try ends up causing an error or is incorrect syntax.
This will not work:
const fieldType = 'relationship';
const updatedItemsArr = [...this.state.targetItems, {fieldType: value}];
Nor will this:
const updatedItemsArr = [...this.state.targetItems, {`${fieldType}`: value}];
How can I pass in a dynamic value here?
>Solution :
In ES6 what you can do is you can put the dynamic key inside brackets. So
const updatedItemsArr = [...this.state.targetItems, {[fieldType]: value}];
should work.