When programming, we often work on arrays and objects. Javascript provides us with methods from Array.prototype that make it easier for us to work on arrays, but sometimes we need more complicated functions. In this article, I'll introduce the lodash library, which has a lot of functions that we won't find in pure Javascript.

LodashJS

What exactly is Lodash anyway?

Lodash is a library written in Javascript and originated as a fork of the underscore.js project. Unlike other libraries of this type, Lodash avoids iterative methods in favor of simplified loops. Lodash is a very popular library among Javascript libraries, when we enter npm we can see it at the top of the list of most downloaded packages. As I mentioned in the introduction, Lodash adds us additional methods that make working on arrays or objects much simpler and reduces the code to single instructions. The Lodash library is ideal for:

  • Iterating arrays, objects and strings 
  • Manipulating and testing values
  • Creating complex functions

 

Are you looking for a contractor working with Lodash ?
logo

Lodash installation

We can install Lodash using npm:

npm install --save lodash

 

Lodash functions

_.concat() - This function connects values to an existing array.

let animals = ['whale', 'vampire bat', 'unicorn'']

let animals = _.concat(arr, 'owl', 'penguin');
// Output => animals = ['whale', 'vampire bat', 'unicorn'', 'owl', 'penguin']

 

_.intersection - This function finds common elements between two arrays.

let animals1 = ['zebra', 'vampire bat', 'owl', 'frog']
let animals2 =  ['whale', 'vampire bat', 'unicorn'', 'owl', 'penguin']

let commonElements = _.intersection(animals1, animals2);
// commonElements-> ["vampire bat", "owl"]

 

_.chunk - Creates an array of elements divided into groups by length.

let animals = ['zebra', 'vampire bat', 'owl', 'frog']

let chunk = _.chunk(animals, 2);
// chunk => [['zebra', 'vampire bat'], ['owl', 'frog']]

Our offer

Web development

Find out more

Mobile development

Find out more

E-commerce

Find out more

UX/UI Design

Find out more

Outsourcing

Find out more

Related articles

Show all articles