The Low Down on Lo-Dash

January 12, 2015 - Jake Bresnehan

If you are a seasoned JavaScript developer or just getting into it you should definitely checkout and try to incorporate a library called Lo-Dash into your tool box.

In a nutshell, Lo-Dash is a super useful library that gives you access to over 100 extremely performant functions to help you avoid reinventing the wheel whilst writing JavaScript.

To get started with Lo-Dash, all you need to do is load the Lo-Dash library like so:

<script src="lodash.js"></script>

The latest release can be found on the GitHub repository, cdnjs or via NPM.

A Couple Of Basic Lo-Dash Examples

With this simply array of objects we can easily see how handy and powerful Lo-Dash is.

var drinks = [
  { 'name': 'Coke', 'quantity': 2 },
  { 'name': 'Red Bull', 'quantity': 6 }

One circumstance that we might come across during development is the need to get all the drink names. By utilising the ._pluck() function this becomes extremely trivial.

var currentDrinks = _.pluck(drinks, 'name');
// → [‘Coke’, ‘Red Bull’]

Another simple use case is to get the drink with the highest stock level. Here we can use ._max() like so:

var maxQuantity = _.max(drinks, 'quantity');
// → { 'name': 'Red Bull', 'quantity': 6 }

Live Example

JS Bin{.jsbin-embed}

See all the Lo-Dash functions

If you have been into JavaScript development for a while you may of also heard about underscore.js which is very similar to Lo-Dash but has some fundamental differences.

I created Lo-Dash to provide more consistent cross-environment iteration support for arrays, strings, objects, and arguments objects1. It has since become a superset of Underscore, providing more consistent API behavior, more features (like AMD support, deep clone, and deep merge), more thorough documentation and unit tests (tests which run in Node, Ringo, Rhino, Narwhal, PhantomJS, and browsers), better overall performance and optimizations for large arrays/object iteration, and more flexibility with custom builds and template pre-compilation utilities.

— John-David Dalton

Source: Stackoverflow

Hopefully this basic introduction above has you intrigued to get your hands dirty and experience the real power with your own eyes.

Join over 30,000 developers & designers who get the best news and articles delivered right to their inbox each week.