Refactoring: Replace Loop with Collection Closure using Lodash

Brett Cassette
InstructorBrett Cassette

Share this video with your friends

Send Tweet

Logic often gets added to our loops. We need to iterate over a collection, and get the sum of values, for instance. This approach can lead to complicated methods, that are hard to test. Using Lodash (Underscore), we are going to extract logic in a large loop body into well named methods. The result will be cleaner and easier to understand.

wojciech
wojciech
~ 7 years ago

I'm not sure if I missed something obvious here, but why the use of _.chain here? Wouldn't _.reduce(rentals, function(sum,renal){return sum + rental.charge()}) produce the sum?