How to replace .Id with a variable string value

Viewed 147

I currently have the following in my Javascript code where Id is a field of an object:

this.recId = event.detail.row.Id;

I want to make the Id part generic something like

String a = 'Id';
this.recId = event.detail.row.a;

How do I achieve this in Javascipt

2 Answers

Use [ ] instead.

const obj = {
  id: "myObject"
}

const a = "id"

console.log(obj[a]);

In your example, it would be

this.recId = event.detail.row[a];

You can use this.recId = event.detail.row[a] to get the desired result

Related