When working with the manipulation of objects, arrays, strings, numbers, etc., we can use a library known as Lodash. This library enables the writing of optimal and simple code by providing lots of utility methods to do so. Besides using a direct CDN link, Lodash can be installed with npm or yarn packet managers. 

If you are using Node.js, you can use the following command to install Lodash:

npm install lodash

In order to use it, it needs to be included in the code file.

const _ = require(“lodash”);

To test if this is working in Node.js, we will create a file named testLodash.js with the following chunk of code:

const _ = require('lodash');
let simpleObject = {
    firstName: 'Mark',
    lastName: 'Smith'
};


// Deep copy
var deepCopySimpleObject = _.cloneDeep(simpleObject);

In the above example, the Lodash utility method will create a deep copy of an object without any additional logic needed.

However, Lodash is not the only way to go regarding object manipulation. Javascript offers many other options, including Ramda, Underscore.js, jQuery, etc. All of these libraries provide various functions, making JavaScript one of the most versatile languages.

Lodash functions

The focus of this article is to explain some useful Lodash functions and how we can use them in our code. Therefore, the following functions have widespread usage in everyday programming. 

1. _.difference()

The _.difference() method in Lodash creates a new array from the existing array by excluding all the common values.

Example:  

let simpleArray = ["a", "b", "c"];
let values = ["b"];
let newArray = _.difference(simpleArray, values);
console.log("New array:" + newArray);

Output:

New array:["a", "c"]


2. _. sortBy()

The _.sortBy() function creates an array of elements sorted in ascending order. This is achieved by running each element in a collection through each iteratee. In addition, Lodash offers a similar function _.orderBy, which gives us the option to sort in ascending or descending order.

Example:

let developers = [
    { 'developer': 'Mike', 'yearsOfExperience': 6 },
    { 'developer': 'Simon', 'yearsOfExperience': 7 },
    { 'developer': 'Joe', 'yearsOfExperience': 5 },
    { 'developer': 'Sheldon', 'yearsOfExperience': 2 }
];
let sortedArray = _.sortBy(developers, ['yearsOfExperience']);
console.log(sortedArray);

Output:

[
  { developer: 'Sheldon', yearsOfExperience: 2 },
  { developer: 'Joe', yearsOfExperience: 5 },
  { developer: 'Mike', yearsOfExperience: 6 },
  { developer: 'Simon', yearsOfExperience: 7 }
]


3. _.flatten() 

Lodash _.flatten function is used to flatten an array to one level deep. This function is beneficial when we have a lot of nested arrays inside one another.

Example:

let originalArray = [[1, 2], ['One', 'Two']]
let newArray = _.flatten(originalArray);
console.log("New array: ", newArray);

Output:

New array:  [ 1, 2, 'One', 'Two' ]


4. _.camelCase() & _.snakeCase()

These two functions are meant for string conversion to desired notation. In contrast to similar string functions, simplicity and ease make them stand out. 

Example: 

let firstString= _.camelCase("I love programming");
console.log("Camel case: " + firstString);
let secondString= _.snakeCase("I love programming");
console.log("Snake case: " + secondString);

Output:

Camel case:iLoveProgramming
Snake case:i_love_programming


5.  _.isEqual() 

Finally, we have _.isEqual() function. This function performs a deep comparison between two values to determine if they are equivalent. Moreover, it supports comparing arrays, array buffers, boolean, date objects, numbers, objects, sets, maps, etc.

Example:

let firstObject = { "name": "Anna" };
let secondObject = { "name": "Anna" };
console.log(_.isEqual(firstObject, secondObject));

Output:

True


Conclusion

Lodash is a powerful library of the JavaScript language. With minimal effort, developers can write clean and efficient code. Hopefully, it will become your go-to library when using JavaScript. 


“Lodash library in Javascript” Tech Bite was brought to you by Vedad Karalić, Junior Quality Assurance Engineer at Atlantbh.

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

selenium
QA/Test AutomationTech Bites
December 22, 2023

Selenium Grid 4 with Docker

Introduction When talking about automation testing, one of the first things that comes to mind is Selenium. Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of…

Want to discuss this in relation to your project? Get in touch:

Leave a Reply