DEV Community

loading...

Discussion on: JavaScript’s Reduce Method Explained By Going On a Diet

Collapse
luqezman profile image
Lucas Silva

let total = foods.reduce((sum, {calories}) => sum + calories, 0); // Single line

Collapse
sara__cruz profile image
Sara Cruz

I don't fully understand why there is a 0 after the parameters, could you explained please? :)

Collapse
citizen428 profile image
Michael Kohl

reduce takes two arguments, a function that calculates the next vaue for the accumulator (sum in this case) and an initial value (0).

Parameter 1: (sum, {calories}) => sum + calories
Parameter 2: 0

Thread Thread
sara__cruz profile image
Sara Cruz

Ok! Now I understand! 🙂 thanks!!