10 common usage of lodash

lodash is a excellent tool library for every javascript developer. It is similar to underscore, but smaller. It is always come in handy to keep it in your toolchain.

map

This should be quite familiar for most JS developers. It iterate through a collection and perform a 1-to-1 mapping.

mapValues

mapValues is just like map but instead of the key being iterate, it use the value (and keeping the key), so it is particularly useful for iterating properties of an object.

reduce

reduce is used when you want to aggregate a collection into a single value, or when you want to iterate and remove some of the element from a collection.

throttle

Throttle is used to wrap a function that is a relatively expensive, and you don’t want to spam-call it.

debounce

Debounce is commonly used to prevent double-click, if the function/process is critical.

get/set

Get and Set feature a simpler way to access deep properties in a object.

find/findKey

Searching is among the most common usage. I’m just going to quote the example from the doc here.

omit/pick

Sometimes you want to remove some of the properties from an object, you can do so by omit (blacklist method)

Or it is also possible with pick (whitelist method)

The chain method

Not many people know lodash provide a method to chain multiple actions nicely. Again, I use the example in lodash doc directly.

 

Using only part of lodash

If you are using lodash in nodejs, or writing JS

In this way, when your use something like webpack, you can keep the bundle size even smaller, because you probably don’t need all the features in lodash.

Share your thoughts