Reduce Method

The reduce() method executes a reducer function for array element. The reduce() method returns a single value: the function's accumulated result. The reduce() method does not execute the function for empty array elements. The reduce() method does not change the original array.
 avatar
Pranav
javascript
3 years ago
463 B
7
Indexable
    
    //array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
    
    
     //to find total price
        
    let items = [
        
        {item:"sample1", price:10},
        
        {item:"sample2", price:15},
        
        {item:"sample3", price:25}
        
        ]
    
    let sum = items.reduce((total, currentValue)=>{
        
        return total+currentValue.price
        
        
    },0)
    
    console.log(sum)
Editor is loading...