如何在Javascript中设置动态道具键?

时间:2018-02-22 09:02:06

标签: javascript

我有值状态,键,值的道具,我可以使用

获取值
this.props. status,
this.props.key,
this.props.value

但我正在寻找一个解决方案 我必须动态传递道具键 例如:

dynamicFunction(variable)
{
let tempValue= this.props.{here I have to pass value}  
console.log(tempValue) 
}

如果this.props.key值为true,则控制台必须打印true 任何解决方案?

2 个答案:

答案 0 :(得分:0)

可以像访问和使用['属性']一样访问每个对象属性。

因此,您可以使用包含您要访问的属性的变量并创建一些内容" dynamic"。

const value = 'key';
let tempValue = this.props[value];

答案 1 :(得分:0)

我假设您想使用参数'variable'传递值,那么这可能对您有帮助。

this.props. status,
this.props.key,
this.props.value

dynamicFunction(variable)
{
let tempValue= this.props[variable];  // here if you call this function with 'status' as parameter it will work.
console.log(tempValue) ;
}