Taking Lodash off the Down Low for New Devs
What is Lodash?
Installation is Easy
Lodash can be installed via NPM or Yarn:
npm install --save lodash
import _ from "lodash"
Now, all of Lodash’s functions can be accessed by the underscore operator.
Here’s an example using the _.concat() function, more about that shorty
let arr = ['dog', 'cat', 'bird', 'hamster']let arr = _.concat(arr, 'zebra', 'snake');// Output => arr = ['dog', 'cat', 'bird', 'hamster', 'zebra', 'snake']
- It’s very readable. The function names are very human friendly with names like
— — — times( ) - as the name implies, this does something x amount of times
— — — omit( )- deletes someithing from an update
— — — compact( )- removes falsey values from arguments passed in
— — — concat( )- concatenates values onto an existing array
— — — find( )- Instead of using a loop to iterate through an array to search for an object, the
_.find() function can perform the same task in a single line. In fact, it can be used to find an object using multiple properties.
- Delegating maintenance of this function to a well maintained package.
- You’re not really compromising much performance when implementing Lodash.
- shorter code, you could be writing the same functions in one liners.
- you can chain functions explicitly or implicitly to do nested arithmetic
- Lastly, one of the great benefits you get from using Lodash is that unlike some of the native .js functions, Lodash functions are supported in every browser
Hope you Learned Something!
As always I would love to hear from you the reader. If you have any feedback or questions, don’t hesitate to reach out.