It's now time to look at reducing collections. Lo-Dash helps a lot here too, supplying functions that help us reduce arrays, objects, and anything that's thrown our way. Aside from primitives, all data structures can be reduced to something simpler.
We'll start off by looking at the common reduce cases, summing values, and such. This will be followed by the topic of filtering collections and how it relates to reducing. Then, we'll look at some more advanced computational techniques.
Unlike other programming languages, JavaScript has no built-in mechanism to sum together an array of values. The closest we get to summing is the native Array.reduce()
method, which is actually general purpose and not specifically for summing values. The Lo-Dash version of reduce
is even more general purpose, and here's an example of how to use it in summing values in a collection:
var collection = [ { ram: 1024, storage: 2048 }, { ram: 2048, storage: 4096 }, { ram...