[SalesForce] How to dynamically get the field value from object result in lightning component

I want to take the field value dynamically in lightning controller . I got the object result but unable to the field value.

({
doInit: function(component, event, helper) {

    var action = component.get("c.getFieldValues");
    action.setParams({
        sObjectName :component.get("v.sObjectName"),
        fieldNames  :component.get("v.fieldNames")
    });

    action.setCallback(this, function(response) {

        var state = response.getState();            
        var fieldArray = [];
        fieldArray = component.get("v.fieldNames").split(',');
        console.log('###'+fieldArray[0]);
        var accFieldName=fieldArray[0];

        if (state === "SUCCESS") {
            var objData = response.getReturnValue();
            console.log('####gotFieldValue'+objData.Name); // this line value is getting values
            console.log('####NoValue'+objData.accFieldName); //this line value is not getting 
        }
    });
    $A.enqueueAction(action);
}

})

Here for objData I am getting the value. If i try to take (objData.Name) the account name is coming .But i want to pass the field value dynamically from
fieldNames.It is not working .

Please help me !!

Best Answer

In JavaScript, to dynamically get field of object you should use obj[FIELD].

Try this line instead:

console.log('####NoValue'+objData[accFieldName]);

Please mark it as solution once it works.

Related Topic