Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const items=[
- { name:'Bike' , price:100} ,
- { name:'TV' , price:200} ,
- { name:'Album' , price:10} ,
- { name:'Book' , price:5} ,
- { name:'Phone' , price:500} ,
- { name:'Computer' , price:1000} ,
- { name:'Keyboard' , price:25}
- ]
- const total=items.reduce((currentTotal , item)=>{
- //it takes an item and a property we want to reduce to
- //a second parameter is a starting point in this case 0
- return item.price + currentTotal;
- },0)
- console.log(total);
- //reduce method runs a function on every membr of the array
- //the first member of that function(currentTotal) is going to be whatever the previous iteration of this array returned
- //the second item(item) is the actual item in the array
- //the currentTotal is going to start on the very first iteration with whatever we pass in as a second parametar(this case 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement