JavaScript: Node & Lodash

(Last Updated On: )

In this tutorial we will be giving out some quick examples of Lodash functionality. To know more go here. There are so many examples and ways of doing Lodash. Check out the documentation.

Q: What is Lodash you ask?
A: It is a toolkit of Javascript functions that provides clean, performant methods for manipulating objects and collections. It is a “fork” of the Underscore library and provides additional functionality as well as some serious performance improvements.

First thing we need to do is install it. You will need a node site already ready to go. If you don’t have one you can follow this tutorial on setting a basic one up.

npm install lodash --save

On whatever page you are working with all you need to do is add the following to where your requires are.

var _ = require('lodash');

Now we can use the functionality as we wish. I will do some basic uses below.

Array difference:

If we want to find the difference of an array to the second array. The result would be “1” because 1 is not in the second array. Notice how it does not compare the second array to the first. It’s only checking which values 2 or 1 don’t exist in the second array.

_.difference([2, 1], [2, 3])
Array uniqWith:

If you want to get the unique items in an array you could use the following. It would return “2 45 3 7 8 1” only notice that the additional 45 is not displayed. It has been removed.

_.uniqWith([2, 45, 3, 7, 8, 45, 1], __.isEqual)