Wednesday, July 27, 2016

Functional JS Helpers: You need Underscore's groupBy

Several posts have been written about libraries like Underscore and Lodash becoming obsolete because of es6 and es.next etc. Well, there are a couple problems with those obituaries: browser support and language support. Throwing a few array helpers in to the spec and implementing some of them does not replace these libraries. There will never be a native version for every single _ function. There probably shouldn't be. Some make sense in the language spec, others make more sense in a helper library.

One of my favorite helper functions is the groupBy function. There are versions of it in Lodash, Ramda, Underscore and elsewhere. Here I will wrap Underscore's version to do something very useful...

I use an image not text so that you will type it out. Trust me. Take the time.



Since every web app is basically JSON objects flowing in that you tinker with and then display, you can see how and why groupBy is so useful. Let's face it, the JSON you get from the server is never exactly how we want it for our UI app. It really shouldn't be, it should be at least somewhat generic so it can be used elsewhere.

In the battle to take what you get from your RESTful services and make it in to something that pleases third party plug-ins etc., groupBy and other functional helpers are priceless.

No comments:

Post a Comment