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
Siva SakthiSiva Sakthi 

How to calculate custom array values in lightning component

Hi,

           I have tried to calculate sum of custom array values in lightning component. But unable to get the index values. If we get the index values means using for loop to calculate the sum. Please guide me where i made mistake and how  to solve this. 

Component:
-----------------------
<aura:component >
    <aura:attribute name="arrvals" type="integer[]" default="1,2,3,4,5"/>
    <aura:attribute name="numbers" type="integer"/>
    
    <aura:iteration var="num" items="{!v.arrvals}" aura:id="ipv" indexVar="index" >
        {!index}<ui:inputNumber label="Input value : " aura:id="{!index+'ip'}" placeholder="Enter the Vlaue"/>  
    </aura:iteration>
    
    <ui:button aura:id="addbtn" label="Calculate" press="{!c.calculate}" /><br/>     
    Sum of the Value : <ui:outputNumber aura:id="totalValue" value="{!v.totalValue}" />  
</aura:component>

Controller.js
------------------
({       
    calculate : function(component) {
       
       try {     
            debugger;
            var numbers=component.find("index+ip").get("v.value");
            console.log(numbers); 
            var arrlist =component.get("v.arrvals");
            console.log(arrvals);            
            var totalval=0;            
            for(var i=0; i<arrlist.length; i++) { 
                //var sum = parseInt(numbers)
                totalval += parseInt(numbers) ;
                console.log(totalval);                
            }          
                component.set('v.totalValue',totalval);            
        }                           
        catch (e) {
            alert('Exception : '  + e);
        }     
    } ,
})

Advance Thanks
Sivasakthi
Ganesh Babu 8Ganesh Babu 8
Hi Siva,

The Indexvar helps to set a number for each row. 
Do you want to find the row number while doing changes on Input box or what is your exact requirement? 

Thanks and Regards,
Ganesh Babu.