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
NickDzitarsNickDzitars 

How to use dynamic property names to access an object.

I am creating a generic list component that takes in an object type, and a list of fields that it wants to display. I am looping through each of the items and building a table, but when it comes to outputting the different fields, I am at a bit of a roadblock for doing it dynamically. Here is the relevant component code: 
<aura:component description="ObjectList" controller="ObjectListController">
    <aura:attribute name="objectName" type="String" default="" />
    <aura:attribute name="objectFieldsString" type="String" default="Id,Name" />
    <aura:attribute name="objectFields" type="List" />
    <aura:attribute name="items" type="List" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <table>
        <thead>
        <tr>
            <aura:iteration items="{!v.objectFields}" var="fieldName">
                    <th scope="col"><div class="slds-truncate">{!fieldName}</div></th>
            </aura:iteration>
        </tr>
        </thead>
        <tbody>
        <aura:iteration items="{!v.items}" var="item">
            <tr>
                <aura:iteration items="{!v.objectFields}" var="field">
                        <td>
                                {!item.field}
                        </td>
                </aura:iteration>
            </tr>
        </aura:iteration>
        </tbody>
    </table>
</aura:component>

In the aura:iteration in the tbody, I am trying to cycle through the different field names, and then grab those fields from the item, but it doesn't seem like lightning supports it. For example if objectFields contained an array of strings containing ["Id","Name"], I want to then grab item.Id and item.Name and display them in the table. But using {!item.field} or anything similar does not seem to work.

Could anyone provide me with the proper syntax if this is possible, or a possible workaround to get something like this to work?
Manoj Maraka 18Manoj Maraka 18
Hey Nick,
I have a similar scenario. Any luck with it?
Taresh KhandekarTaresh Khandekar
You can add a design attribute 
<design:attribute name="objectName" label="Object Name"/> 
and set that attribute while dploying component in properties.