Says my state is like this:
{
item:{
a:'a',
b:'b'
}
}
Then I'm able to pull a from item by doing:
const { a } = this.state.item
but can pull dynamically using {} of es6 ?
For example const { variable } = this.state.item, where variable can be a or b.
As 4castle pointet out, you could use Computed object property names and destructuring with an additional key/value pair variables for destructuring.
var object = { item: { a: 'a0', b: 'b0' } },
key = 'b',
value;
({ [key]: value } = object.item);
console.log(value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With