0

I'm trying to access the index key of a hash object via the aura expression, but it seems to return nothing/undefined. The index key was manually set via helpers through Array#Reduce.

I know I can get the index while doing an aura:iteration, but I'm dealing with a 2 dimensional array (e.g. [[object, object], [object, object]].

Below is an example:

controller:

initializeComponent: function(component, event, helper){
    var array = component.get('v.myList');
    var twoDimensionalArray = helper.createTwoDimensionArrray(array);
    //this helper function is reducing the array via Array#Reduce.

    component.set('v.newList', twoDimensionalArray);
}

cmp file:

<aura:component>

  <aura:attribute name="myList" type="List[]" access="global" />
  <aura:attribute name="newList" type="List[]" access="private" />

  <aura:iteration items="{!v.newList}" var="list">
     <aura:renderIf isTrue="{!list.completed != 'true'}" >
       <aura:iteration items={!list.todos} var="todo">
          {!todo.index} : {!todo.name}
       </aura:iteration>
     </aura:renderIf>
  </aura:iteration>

</aura:component>

I feel like the data is being mutated by Lightning and Aura, is there a better way to do this? Am I missing something?

  • How confident are you that the array is actually populated ? – CloudHugger Aug 15 '16 at 21:49
  • @CloudHugger pretty confident since {!todo.name} is being printed out on the page. I did find a solution for this. I created a new context of each object as I iterate over each object. After I have set the attribute to the component, it now prints the index. – kev_panda Aug 15 '16 at 23:12
0

I had a workaround which was to return a new hash object for each element in the array as I iterate and reduce the data. I believe because the attributes are coming from the parent, it's making it immutable. Which I personally think is great.

|improve this answer|||||

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.