Topics In Demand
Notification
New

No notification found.

13 Lodash Functions to Replace with Pure JavaScript
13 Lodash Functions to Replace with Pure JavaScript

September 16, 2022

397

0

In our coding journey, we strive for perfection. During this eternal journey, we may face situations where there is a need to optimize our written code. This endeavor may result in lengthy and bulky lines of code.

Opting for useful libraries such as Lodash to optimize code seems like a good option in such instances. We follow advanced terminology (like for e.g., external libraries) to avoid writing complex lines of code comprising many validations, loops, and if-else conditions.

Lodash provides many useful functions for arrayscollection, object, string, Math, Number, Lang, Date, etc.

But, JavaScript, as a stand-alone platform, has very useful features and functions that can be used instead of Lodash functions. Today we examine a few of these functions.

Here are 13 Lodash functions that can be replaced with Javascript functions:

1> _.assignIn()

We use this function to add a new property to an object. Additionally, we can also assign the property of one object to another object. JavaScript also has provisions for these kinds of requirements. We can achieve this with the following methods:

let first_object = {name: “john”} ;let second_object = {location: “California”};

Table

Description automatically generated

Output:

{_id: 1, profile: “engineer”, name: “john”, location: “California”}

2> _.values()

This function comes in handy to obtain the values of each property in the object collection. We can use JavaScript as it has a compatible function, with the same name, to achieve this functionality.

var users = {‘Amar’: { ‘emp_id’: 001, ‘location’: ‘India’},‘Umar’: { ‘emp_id’: 002, ‘location’: ‘Canada’},‘John’: { ‘emp_id’: 003, ‘location’: ‘USA’},‘Rishi’: {‘emp_id’ :004, ‘location’: ‘USA’} };

Output:

{emp_id: 1, location: “India”} {emp_id: 2, location: “Canada”}{emp_id: 3, location: “USA”}{emp_id: 4, location: “USA”}

3> _.pick()

As the name suggests, the pick function is used to collect a property in an object. We can choose to pick multiple properties from the object, and it will return the response in the object.

Let us use the sample data from ‘Users’ example in point 2:

Table

Description automatically generated

Output:

{Amar: {emp_id: 1, location: “India”}John: {emp_id: 3, location: “USA”} };

4> _.omit()

Omit is used to remove the selected property from the input and give the new object in the response. We can use simple JavaScript code to obtain this result as well.

We will use the above User example:

Output:

{John: {emp_id: 3, location: “USA”}Rishi: {emp_id: 4, location: “USA”}}

5> _.compact(): Array

Compact is an ideal function to sanitize an array. This is function is used to remove all unrecognized values from the array. We can handle values such as null, false, undefined, Nan, 0, using compact.

var arr = [12,’hi’,undefined, 0,12.9, null,”];

In JavaScript, we can use the filter method to remove the unrecognized values from an array.

Output:

[12, “hi”, 12.9]

6> _.uniq():Array

The uniq function is also an array-based utility. This function is great for eliminating the repeated values from an array.

let arr =[2,4,6,”as”,”as”,8,-8,-9,-9,0,0,null,null];

Output:

[2, 4, 6, “as”, 8, -8, -9, 0, null]

7> _.find()

The find function is one of the most commonly and frequently used functionality. As the name suggests, it is instrumental in finding an element in a collection of data. We use this function to obtain the first object in the collection, provided it satisfies the condition we set.

let emp_data= [ {emp_id: 1, location: “India”},{emp_id: 2, location: “Canada”},{emp_id: 3, location: “USA”},{emp_id: 4, location: “USA”}]

Output:

{emp_id: 3location: “USA” }

8> _.filter()

The filter function is very close to the find function that we discussed earlier. This function too provides results by filtering all elements based on the condition we provide. We can procure all the elements from a collection that satisfy our specific condition. We will follow the above example for the filter function.

Output:

[{emp_id: 3,location: “USA” }]

9> _. every()

The Every function is a conditional function that acts as a scanner for all the elements. We provide an operational condition, and this function checks and provides a result (in either True or False) of all the elements that adhere to the condition.

We will follow the sample data for the emp_data example:

let emp_data= [ {emp_id: 1, location: “India”},{emp_id: 2, location: “Canada”},{emp_id: 3, location: “USA”},{emp_id: 4, location: “USA”}]

Output: false

10> _. Some()

We can consider the Some function as a selective version of Every function. We provide an operational condition, and this function checks and provides a result (in either True or False) if at least one of the elements in the array adheres to the provided condition.

We will follow the same ‘emp_data’ for reference, from point 9:

Output: true

11> _. includes()

This function is useful to check whether a specific element exists in the collection. This function accepts string data type as well as an array data type.

We will follow the same emp_data for reference:

Output: true

12> _.flatten()

If we need to bring a nested array onto the same level, then we can achieve this using a JavaScript function with the same name (flatten), instead of using the lodash function.

Output: [1, 2, Array(2), 5]

13> _.difference()

We can use JavaScript to get the difference between two different arrays. Lodash has a defined method to get the difference, but in JavaScript, we use the filter and include methods to get the difference.

Output: [8,7,1]

Hope this information helps you choose better options when using JavaScript in various scenarios.

Happy Coding!

References: https://lodash.com/doc


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


DLT Labs™ is a global leader in the development and delivery of enterprise blockchain technologies and solutions, as well as a pioneer in the creation and implementation of standards for application development. With a deep track record in innovation and one of the world's largest pools of highly experienced blockchain experts, DLT Labs™ enables the transformation and innovation of complex multi-stakeholder processes.

© Copyright nasscom. All Rights Reserved.