Recently, a fellow developer asked me for some help to see if there was an easier way to filter a collection of objects than just writing a long for loop with some logic. My instincts had us head straight to the jQuery documentation site, and we were not disappointed. Their jQuery docs are some of the best we’d seen. Nicely categorized, information on all properties, example code, and demos provided.
Sifting through the Array/Object operations section we came across the “grep” utility method. With the “grep” method, you can easily filter any array with a callback function that returns a boolean. Return true to keep the element and false to remove it. A simple solution to a simple, yet common, problem. This is all the developer really needed but he was probably over-thinking it.
The lesson? Well, to put it bluntly: Documentation is your best friend. I’ve always felt that, most of the time, a simple, built-in method (or set of methods) is all you need to solve a problem. Or at least the common ones I get asked about. Do yourself a favor, bookmark the docs, and don’t be afraid to embrace it. Make it a habit. A good one.