array reduce method is useful :)

In the D3 documentation, Mike Bostock writes: 

When using D3—and doing data visualization in general—you tend to do a lot of array manipulation. That’s because D3’s canonical representation of data is an array. Some common forms of array manipulation include taking a contiguous slice (subset) of an array, filtering an array using a predicate function, and mapping an array to a parallel set of values using a transform function. Before looking at the set of utilities that D3 provides for arrays, you should familiarize yourself with the powerful array methods built-in to JavaScript.

I’ve been impressed with how much I can do if I use these built in methods.  And impressed by how anything I learn about these built in methods immediately make my life easier (and my programming more effective).  But, there are still some that I’m under using or not using at all.  When reading someone else’s code today, I noticed them using “reduce” quite effectively.  

MDN explains reduce here.  

I can’t wait to use this for doing things like finding the max of an array, but for more complicated comparisons than max.  In the past, when facing operations like this, I’ve been using “forEach” instead with a variable that I initialized outside of the forEach as the previousValue.  This will be much cleaner.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s