Reputation: 29
Has anyone tried getting a particular value inside a prop using a variable?
Normally, getting a particular data in props goes like
const dataValue = props.table.data.rowData.account_number
However in this instance I need the last part of the props to be a variable, because
the account_number
value is not fixed and the name varies.
So is there a way that I use a variable instead of adding a fixed name on the past part of the prop call?
Like so:
let theVariable = *somethingNew*;
const dataValue = props.table.data.rowData.theVariable
Upvotes: 0
Views: 801
Reputation: 12787
Yes. You can do like this:
let theVariable = *somethingNew*;
const dataValue = props.table.data.rowData[theVariable];
Upvotes: 0
Reputation: 2244
You can do this through property bracket access:
let theVariable = somethingNew;
const dataValue = props.table.data.rowData[theVariable]
This can also be used during object creation:
const dataValue = {
[theVariable]: {}
}
Upvotes: 2