The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Refactoring: Replace Loop with Collection Closure using Lodash

Refactoring: Replace Loop with Collection Closure using Lodash

6:49
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.
Watch this lesson now
Avatar
egghead.io

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.

Avatar
Laci

I could only hear audio from the left speaker of my headphone. Please be mindful of that issue in the future.

Also, you might wanna take a look at http://ramdajs.com/ - it's a purely functional library unlike Lo-Dash or Underscore. I personally like it better.

Keep it up!

In reply to egghead.io
Avatar
wojciech

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?

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?