Kestal
Kestal

Reputation: 223

How to dynamically pass property

I have the following function. When this function triggers action is being passed as an argument to this function. I want to use the action parameter to dynamically call a property.

The following is where I'm stuck.

onChange(event: any, id: number, action: any){
     this.action[id]["Selected"] = true
}

If I have to manually do it, it will look something like this.

  onChange(event: any, id: number, action: any){
    if( action == "StatutoryReq"){
      this.StatutoryReq[id]["Selected"] = true
    } else if( action == "StatutoryReqErp2007"){
      this.StatutoryReqErp2007[id]["Selected"] = true
    }
  }

Upvotes: 0

Views: 227

Answers (1)

Ravi Kumar Gupta
Ravi Kumar Gupta

Reputation: 1798

In Javascript/Typescript,

obj.some_fn

is same as

obj['some_fn']

So you should be able to use this[action].

onChange(event: any, id: number, action: any){
     this[action][id]["Selected"] = true
}

Upvotes: 2

Related Questions