Discussing Underscore.js at Tuesday’s Node.js DC Meetup

Blog

Estimated
2 min read

At next week’s Node.js meetup here in Washington, DC, I’ll give a brief introduction to Underscore.js, a functional programming library for Javascript. At its base, Underscore.js is about data structures (usually arrays) and interacting with them more efficiently. It also forms the basis or inspiration for several other popular libraries, such as Backbone, and learning to make use of it properly will allow you to segue this knowledge into other interesting areas.

While this library has traditionally been associated with the browser environment, it is still widely used and very applicable to the server side of the equation. As with most node modules, Underscore.js can be installed using the npm package management system. It’s the single most depended on package available on npm, something that illustrates just how many people consider it a basic part of their Javascript programming arsenal.

I’ll go into more detail at the Node.js meetup happening on Tuesday, September 20 at 7:00 pm in our garage here in Washington, DC. I’m also including more details on Underscore below for those who can’t wait.

Some underscore basics

A common situation in which you might use Underscore.js is for retrieving records from your datastore of choice (we prefer CouchDB, but it could be whatever).

`var countries = [
	{ "iso3": "AFG", "name": "Afghanistan"},
	{ "iso3": "ALB", "name": "Albania", "active": true },
	/* trimmed for brevity */
	{ "iso3": "ZMB", "name": "Zambia" },
	{ "iso3": "ZWE", "name": "Zimbabwe", "active": true }
]`

Say you want to filter the list of countries to only include the items that have the active property set to true. This can be easily done through the following code that makes use of the select method, which will step through your objects and pass each one to the test function you supply, returning a new array containing only the objects that passed the test.

`var activeCountries = _(countries).select(function(country) {
	return country.active === true;
});`

Now say you want to have just the id or iso3 code, so you can make use of the array to do input validation. Underscore provides the wonderfully useful pluck method which will step through your objects and return an array containing the values of the property you requested.

`// Build an array of only valid country codes
var validISO3 = _(activeCountries).pluck('iso3');`

Then you can use the include method to test for the validity.

`// check if a submitted value is allowed :
var success = _(validISO3).include(req.params.iso3);`

These are just some of the basic things you can do with the library. It is filled with little gems that help you do all kinds of programming tasks. The entire library fits into a 3k file when minimized, and considering its small size and inherent power, there’s really very little reason not to make use of it both on the server and the client side.

What we're doing.

Latest