function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ranjan Kumar 20Ranjan Kumar 20 

how to access object values in lightning component

I want to render a table dynamically in lightning. I have a list of data of transaction (Custom object) and list of string which contains API name which data i want to render.
I have created a child component and wrapped it in nested aura iteration to display the data. But I am unable to access the value.
 
aura:iteration items="{!v.Transactions}" var="trans">
       <tr>
       <aura:iteration items="{!v.fieldNames}" var="field" indexVar="ind">
         <td><c:TransactionOutputField object="{!trans}" fieldName="{!field}"/></td>
       </aura:iteration>
       </tr>
     </aura:iteration>


child component
___________________

 
<aura:component>
  <aura:attribute name="object" type="Object" />
  <aura:attribute name="fieldName" type="String" />
  <aura:attribute name="fieldVal" type="String" access="private" />
  <aura:handler name="init" value="{!this}" action="{!c.myAction}"/>

  {!v.fieldVal}
</aura:component>


controller
__________
 
({
    myAction : function(component, event, helper) {

        var obj = component.get("{!v.object}");
        var field = component.get("{!v.fieldName}");

        console.log(JSON.stringify(obj));
        console.log(field);

        var value = obj.field;
        console.log(value);

        component.set("v.fieldVal", value);

    }
})


debug value in the obj :
 
{"Id":"a0l1q000000BYLQAA4","Name":"0002000332","akritiv__Amount__c":0,"akritiv__Balance__c":0,"akritiv__Source_System__c":"SAPG","akritiv__Batch_Number__c":"20181003124027"}


debug value in the field :
 
akritiv__source_system__c




 
Amit GhadageAmit Ghadage
Hi Ranjan,

You can access object property using key value pair in Javascript.
Like : String fieldValue = object["fieldName"];

Please refer below JS controller code.
({
    myAction : function(component, event, helper) {

        var obj = component.get("v.object"); // changed to access attribute values
        var field = component.get("v.fieldName"); // changed to access attribute values

        console.log(JSON.stringify(obj));
        console.log(field);

        var value = obj[field]; // Changed line to access object field.
        console.log(value);

        component.set("v.fieldVal", value);

    }
})


 
Ranjan Kumar 20Ranjan Kumar 20
Hey Amit,

I am only getting the value for the Name field. And for other custom field I am getting "Undefined"
 
{"Id":"a0l1q000000BYLPAA4","Name":"0002000331","akritiv__Amount__c":400,"akritiv__Balance__c":400,"akritiv__Days_Past_Due__c":358,"akritiv__Source_System__c":"SAPG","akritiv__Batch_Number__c":"20181003124027"}
components/c/TransactionOutputField.js:17 Name
components/c/TransactionOutputField.js:22 0002000331
components/c/TransactionOutputField.js:16 {"Id":"a0l1q000000BYLPAA4","Name":"0002000331","akritiv__Amount__c":400,"akritiv__Balance__c":400,"akritiv__Days_Past_Due__c":358,"akritiv__Source_System__c":"SAPG","akritiv__Batch_Number__c":"20181003124027"}
components/c/TransactionOutputField.js:17 akritiv__balance__c
components/c/TransactionOutputField.js:22 undefined
components/c/TransactionOutputField.js:16 {"Id":"a0l1q000000BYLPAA4","Name":"0002000331","akritiv__Amount__c":400,"akritiv__Balance__c":400,"akritiv__Days_Past_Due__c":358,"akritiv__Source_System__c":"SAPG","akritiv__Batch_Number__c":"20181003124027"}
components/c/TransactionOutputField.js:17 akritiv__source_system__c
components/c/TransactionOutputField.js:22 undefined

 
Amit GhadageAmit Ghadage
Hi Ranjan,
I tried below code snipped and it is showing me correct values
 
var o = JSON.parse('{"Id":"a0l1q000000BYLPAA4","Name":"0002000331","akritiv__Amount__c":400,"akritiv__Balance__c":400,"akritiv__Days_Past_Due__c":358,"akritiv__Source_System__c":"SAPG","akritiv__Batch_Number__c":"20181003124027"}');
  console.log("o.akritiv__Days_Past_Due__c : "+ o["akritiv__Days_Past_Due__c"]);

Output : 

o.akritiv__Days_Past_Due__c : 358
May be your browser chaching is enabled so values in debug are not getting refreshed.

Can you disable "Enable secure and persistent browser caching to improve performance" from session sessting and refresh page one more time.