0

I have props with value status, key, value and I am able to get the value using

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

But I am looking for a solution that I have to dynamically pass the props key For eg:

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

if this.props.key value is true then console have to print true
Any solutions?

ChrisR
  • 3,922
  • 1
  • 14
  • 24
praj
  • 849
  • 1
  • 16
  • 39

2 Answers2

0

Each object property can be accessed like you access and Array, with ['property'].

So you can use a variable containing the property you want to access and create something "dynamic".

const value = 'key';
let tempValue = this.props[value];
ChrisR
  • 3,922
  • 1
  • 14
  • 24
0

I assume you want to pass the value using the parameter 'variable' then this might help you.

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) ;
}
HungrySoul
  • 1,151
  • 2
  • 17
  • 31