Most Common Lodash Functions A Developer Should Use

    Jul 20, 2020       by Pankaj Kumar
most-common-lodash-functions.jpg

For a developer, It's not only necessary to perform the task but also to perform task in smarter way. There are alot of libraries in javascript today which makes the task very easy for developers while playing with varialbles. In this article we will understand the use of most common functions of Lodash library.

 

What is Lodash?

Lodash is a JavaScript library that provides utility functions for common tasks. It has built-in utility functions which make the code in javascript easy and short.

 

Installation

Lodash can be installed using below commands:

npm install lodash

 

OR

bower install lodash

 

Lodash can be used in client side app or server side applications like below:

// Server side
const _ = require('lodash');

// Client side

// lodash will be exposed as _

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

 

1. _.forEach

This function iterates over an array of items similar to for loop. See the example below:

let users = [

       { firstName: 'John', lastName: Doe'},

       { firstName: 'Gill', lastName: 'Smith'}

];

 

Now using lodash forEach function, we will iterate the users array and add a new key inside object after concatenating the firstName and lastName.

let _ = require('lodash');

_.forEach(users, function(user, index) {

    user.fullName = user.firstName + ' ' + user.lastName;

});

// Output

var users = [

       { firstName: 'John', lastName: Doe',fullName:'John Doe'},

       { firstName: 'Gill', lastName: 'Smith',fullName:'Gill Smith'}

];

 

2. _.uniq

This function takes an array as an argument and return all unique values after removing the duplicate values

let items = [16, 15, 18, 16, 19, 15, 15, 5, 16];
items = _.uniq(items);

// Output [16, 15, 18, 19, 15, 5]

 

3. _. sortedUniq

This function preforms the task of providing the unique values with sorted manner.

const items = [1, 1, 2, 3, 3, 3, 5, 8, 8];

const result = _.sortedUniq(items);

// Output-> [1, 2, 3, 5, 8]

 

4. _.find

This method helps to find the items from an array on the base of some criteria. See the example below:

const users = [

{ firstName: "Chris", lastName: "Doe", age: 20 },

{ firstName: "John", lastName: "Doe", age: 10},

{ firstName: "Jim", lastName: "Carrey", age: 54 }

];

 

let user = _.find(users, { firstName: "Chris" });

// user -> { firstName: "Chris", lastName: "Doe", age: 20}

var underAgeUser = _.find(users, function(user) {

return user.age < 18;

});

// underAgeUser -> { firstName: "John", lastName: "Doe", age: 10 }

 

5. _.assign

This function is equivalend to ES6's Spread Operator. It is used to assign properties of one or many objects to a source object.

let obj1 = { firstName: "Ram" };

let obj2 = { age: 24, address: "XYZ Street, main city, ACC" }

 

let result = _.assign({ salary: 1000000 }, obj1, obj2);

console.log(result);

// output: Object {salary: 1000000, firstName: "Ram", age: 24, address: "XYZ Street, main city, ACC"}

 

6. _.includes

This function returns a boolean value if the second argument is contained in the provided array(first argument);

let names = ["apple", "orange", "mango"];

 

let result = _.includes(names, "mango");

console.log(result);

// Output : true

 

7. _.keyBy

When we need to find a key with specific value. Normally we need to run a loop over the array but with the help of this function task can be done with one go.

let posts = [

{ id: "1", title: "Dummy First blog post"},

{ id: "2", title: " DummySecond blog post"},

{ id: "3", title: "Dunny third blog post" }

];

posts = _.keyBy(posts, "id");

let post = posts["34abc"]

// post -> { id: "3", title: "Dunny third blog post" }

 

8. _.difference

This function helps to find the difference between two array. Here new array will contain the items which is nt present in the second array.

let arr1 = ['mango', 'apple', 'orange'];

let arr2 = ['orange', banana];

let diff = _.difference(arr1, arr2);

// Output: ['mango','apple'];

 

9. _.intersection

This function returns a new array with the common items from the provided two arrays.

let arr1 = ['mango', 'apple', 'orange'];

let arr2 = ['orange', banana];

let matched = _.intersection(arr1, arr2);

// Output: ['orange'];

 

Conclusion

Use fo the above lodash function mentioned can improve the coding standard a lot while coding in JavaScript.

 

Let me know your thoughts over email pankaj.itdeveloper@gmail.com. I would love to hear them and If you like this article, share it with your friends.

Thanks!


WHAT'S NEW

Find other similar Articles here: