+ Start a Discussion
Jayesh Babu A VJayesh Babu A V 

use variable to get a row data from datatable

Iam new to salesforce and I want to get the Name field from a datatable inside my controller js class. I used the following code to do it:
var row = event.getParam('row');
var name = row.Name;
System.debug(name);
Everything is working fine. I am able to get the name data properly. But, my question is, is there any way to replace the field name 'Name' with a variable? I tried like this:
var row = event.getParam('row');
var nameC = component.get("v.nameColumn");//value of nameColumn is 'Name'
var name = row.nameC;
System.debug(name);

But, i didnt work.  In this case, the value of 'name' is undefined. So, is there any other way to use variable as the field name instead of statically typing the field name?
Thanks very much.
Best Answer chosen by Jayesh Babu A V
David Zhu 🔥David Zhu 🔥
You may need to access the field by idex. 

var row = event.getParam('row');
var nameC = component.get("v.nameColumn");//value of nameColumn is 'Name'
var name = row[nameC];
System.debug(name);