2

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.

2
  • do you want to iterate over list and get its index? Commented Jan 25, 2017 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. Commented Jan 25, 2017 at 11:25

1 Answer 1

5

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>
1
  • 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. Commented Jan 25, 2017 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .