es6的解构但传递动态变量 [英] Destructuring of es6 but passing dynamic variable
本文介绍了es6的解构但传递动态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的州
是这样的:
{
item:{
a:'a',
b:'b'
}
}
然后我可以通过以下方式从项目中提取 a
:
Then I'm able to pull a
from item by doing:
const {a} = this.state.item
但可以使用 {}
es6
?
例如 const {variable} = this.state.item
,其中变量可以是 a
或 b
。
For example const { variable } = this.state.item
, where variable can be a
or b
.
推荐答案
4castle pointet out,你可以使用计算对象属性名称和解构以及用于解构的附加键/值对变量。
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);
这篇关于es6的解构但传递动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文