Lodash is a powerful utility library that makes your life easier when manipulating arrays, strings, objects, and much more. You have most likely used it in your projects before (and if not, you definitely should!), but have you used it to its full extent?
Based on what I've seen in the field and from my own experience, it seems like a lot of developers use the popular methods (_.filter
, _.remove
, _.each
, etc) but neglect to look at all the methods and see what can be useful to them. So, without further ado, here's a list of some lesser known lodash
methods that I found very useful, with hope that you can benefit from it:
_.uniqBy
_.uniqBy(array, [iteratee=_.identity])
returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The result order is not sorted based on any specific property, but by the order the values occurred in the array.
The iteratee can either be a property or a function. Here's an example of how you would use it to determine the number of unique visits requested:
var totalUniqueVisits = _.uniqBy(bookedViewings, 'user').length();
_.debounce
_.debounce(func, [wait=0], [options={}])
creates a debounced function that delays invoking func
until after wait
milliseconds have elapsed since the last time the debounced function was invoked.
For instance, if you want to display a thumbnail when a user hovers over a video's timebar, it's preferable to wait until the cursor has stopped moving for a few milliseconds before showing the thumbnail. This way, you avoid processing the thumbnail at every coordinate change while the user is navigating to the specific time they want.
PS: there's a similar method called _.throttle, which could be of better use for your specific situation.
_.isObject and _.isObjectLike
_.isObject(value)
is an easy one, but could be tricky.
From the official documentation: Checks if value
is the language type of Object. (e.g. arrays, functions, objects, regexes, new Number(0), and new String('')).
_.isObjectLike(value)
might be more useful, depending on your use case. This method checks if value is object-like, meaning that it is not null and has a typeof
result of "object".
_.maxBy, _.minBy, and _.sumBy
_.maxBy
, _.minBy
, and _.sumBy
work similar to the _.uniqBy
method we discussed above, where you pass in an array and an iteratee, which is used as the criteria to rank each element in the array. The result is the maximum value, minimum value, or the sum, respectively.
_.pick
_.pick(object, [props])
returns an object comprised of the requested properties ([props]
) only. This can be used to easily extract the properties you need from a user-submitted form, and ignore everything else:
var submittedUser = _.pick(request.body, ['firstName', 'lastName', 'email', 'username'])
PS: The opposite of _.pick
: _.omit
_.chain
_.chain(value)
creates a lodash
wrapper around value, allowing you to call more lodash
methods sequentially. The result must be unwrapped by calling .value()
. Here's an example from the official documentation:
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
var youngest = _
.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
.value();
// => 'pebbles is 1'
Conclusion
If at least one of these methods helps you in an upcoming project or through refactoring existing code, I'll be happy. Please feel free to share additional methods that I may have forgotten about - I'll try to keep this list as up to date as possible.