s

typescript get sum of array elements edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 02 September 2020 | 3578

How to find the sum of elements in an array in angular. In the typescript code below, typescript has an inbuild reduce method to get the sum of array elements. A reduced method reduces the array to a single value.

Sample Script

this.subTotal = this.salesReturnDetail.reduce((sum,current)=> sum   current.ReturnTotal,0);
                    

reduce takes two parameters

array.reduce(callback[, initialValue]);
callback - Function to execute on each value in the array.
initialValue - Object to use as the first argument to the first call of the callback.