1

I have a list of values on which I want both to:

  • Iterate with aura:iteration
  • Get somewhere else in my component specific values based on the index in the list

From what I understand, I can get a specific value with a map, but not iterate over it.
I can iterate over a list but not get a specific value with the index.

As an example, it would looks like something like this:

<aura:component >
    <aura:attribute name="myList" type="List" />

    <!-- What I want 1: iterate -->
    <aura:iteration var="listItem" items="{!v.myList}" indexVar="index">
        {!v.listItem}
    </aura:iteration>

    <!-- What I want 2: Outside of the iteration, get some elements by index -->
    {!v.listItem[2]}

</aura:component>

Is there a way to do this ? Of course I would like to avoid having 2 attributes with 2 types but the same values in it.

  • do you want to iterate over list and get its index? – Manjot Singh Jan 25 '17 at 11:12
  • No, I want to get specific values for a given index. I could do it with a Map, but then I don't think I could still iterate on it too. – Fabien Taillon Jan 25 '17 at 11:25
4

Both will work. It seems to me as some syntax errors;

<aura:component >
    <aura:attribute name="myList" type="List" />

    <!-- What I want 1: iterate -->
    <aura:iteration var="listItem" items="{!v.myList}" indexVar="index">
        //{!v.listItem}
        {!listItem}//Change: This is not an attribute. So 'v.' is not required.
    </aura:iteration>

    <!-- What I want 2: Outside of the iteration, get some elements by index -->
    //{!v.listItem[2]}
    {!v.myList[2]}//Change: 'myList' is the original attribute. 'listItem' is just local to 'aura:iteration'(in this case)

</aura:component>
| improve this answer | |
  • I wrote this code for StackExchange, I've changed my real code in the meantime so I guess it was a typo issue too. Thanks. – Fabien Taillon Jan 25 '17 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.