JavaScript Utility Functions That We Should Stop Rewriting
Posted By : Sachin Arora | 16-Dec-2017
While working with javascript applications, we are often found rewriting the utility functions while dealing with strings and objects in collections iterating problems. So late there came a javascript library which helps us fighting these rewriting problems LODASH. Lodash is library that provides clean and performant utility methods.
Below are few cases/examples that shows how this minified javascript library lodash helps us dealing with this code rewriting problem:-
1. Loop For N Times.
// 1. Basic for loop.
for(var i = 0; i < 5; i++) {
// ....
}
// Lodash
_.times(5, function(){
// ...
});
above example shows how using _.times serves the same purpose of for loop without using an extra variable.
2. Loop through a collection and return a deeply-nested property from each item.
// Fetch the name of the first pet from each owner
var ownerArr = [{
"owner": "Colin",
"pets": [{"name":"dog1"}, {"name": "dog2"}]
}, {
"owner": "John",
"pets": [{"name":"dog3"}, {"name": "dog4"}]
}];
// Array's map method.
ownerArr.map(function(owner){
return owner.pets[0].name;
});
// Lodash
_.map(ownerArr, 'pets[0].name');
Lodash's map method works exactly same like Javascript array method except that it has a upgrade. It is able to navigate deeply-nested property by only providing a string instead of a callback function.
3. Deep-Cloning Javascript Object
var objA = {
"name": "colin"
}
// Normal method? Too long. See Stackoverflow for solution:
how-to-deep-clone-in-javascript
// Lodash
var objB = _.cloneDeep(objA);
objB === objA // false
4. Getting Random Number Between A Range.
// Naive utility method
function getRandomNumber(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomNumber(15, 20);
// Lodash
_.random(15, 20);
5. Merging Two objects together
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
We can also use this _.merge to merge two arrays into a single one by just passing both as arguments.
So,above examples shows that using this lodash library can make our work and code much clearer and easier.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sachin Arora
Sachin is ambitious and hardworking individual with broad skills,He have the capabilities to build captivating UI applications . One of his key strength is building a strong bond with the team in order to deliver the best results on time .