What is lodash?
Lodash is a JavaScript library that provides utility functions for common programming tasks using a functional programming paradigm. It includes functions for manipulating and traversing arrays, objects, and strings, as well as utilities for functions, language, math, number, object, sequence, and utility methods.
What are lodash's main functionalities?
Array Manipulation
Lodash provides a rich set of array manipulation functions such as map, filter, find, and sort. The code sample demonstrates sorting an array in ascending order using a custom comparator.
[3, 2, 1].sort(_.compareWith(function(a, b) { return a - b; }))
Object Manipulation
Lodash allows for easy manipulation and traversal of objects. The code sample shows how to assign properties from source objects to a destination object.
_.assign({ 'a': 1 }, { 'b': 2 }, { 'c': 3 })
String Manipulation
Lodash includes functions to manipulate strings, such as converting to different cases, trimming, padding, etc. The code sample demonstrates converting a string to kebab-case.
_.kebabCase('Foo Bar')
Function Utilities
Lodash provides function utilities like debounce and throttle to control function invocation. The code sample shows a debounced function that will only be invoked after 250 milliseconds have passed without it being called again.
_.debounce(function() { console.log('Debounced'); }, 250)
Language Utilities
Lodash includes utilities for deep cloning, merging, and comparing objects. The code sample demonstrates deep cloning an object to ensure nested objects are cloned as well.
_.cloneDeep({ 'a': 1, 'b': { 'c': 2 } })
Other packages similar to lodash
underscore
Underscore is a utility library with similar functionality to Lodash, offering a range of functions for manipulating arrays, objects, and functions. It is generally considered to be the predecessor to Lodash, which provides a superset of Underscore's features with additional performance optimizations.
ramda
Ramda is a functional programming library that emphasizes a more functional and composable approach compared to Lodash. It provides similar utilities but focuses on immutability and side-effect free functions, which can lead to a different programming style.
immutable
Immutable.js offers a different take on data manipulation by providing persistent immutable data structures. Unlike Lodash, which works with standard JavaScript objects and arrays, Immutable.js uses its own data structures, which can lead to better performance and easier reasoning about state changes in certain applications.
lodash v3.6.0
The modern build of lodash exported as Node.js/io.js modules.
Generated using lodash-cli:
$ lodash modularize modern exports=node -o ./
$ lodash modern -d -o ./index.js
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash
In Node.js/io.js:
var _ = require('lodash');
var array = require('lodash/array');
var chunk = require('lodash/array/chunk');
See the package source for more details.
Note:
Don’t assign values to the special variable _
when in the REPL.
Install n_ for a REPL that includes lodash by default.
Module formats
lodash is also available in a variety of other builds & module formats.
Further Reading
Features
- ~100% code coverage
- Follows semantic versioning for releases
- Lazily evaluated chaining
- _(…) supports intuitive chaining
- _.add for mathematical composition
- _.ary & _.rearg to change function argument limits & order
- _.at for cherry-picking collection values
- _.attempt to execute functions which may error without a try-catch
- _.before to complement _.after
- _.bindKey for binding “lazy” defined methods
- _.chunk for splitting an array into chunks of a given size
- _.clone supports shallow cloning of
Date
& RegExp
objects - _.cloneDeep for deep cloning arrays & objects
- _.create for easier object inheritance
- _.curry & _.curryRight for creating curried functions
- _.debounce & _.throttle are cancelable & accept options for more control
- _.fill to fill arrays with values
- _.findKey for finding keys
- _.flow to complement _.flowRight (a.k.a
_.compose
) - _.forEach supports exiting early
- _.forIn for iterating all enumerable properties
- _.forOwn for iterating own properties
- _.inRange for checking whether a number is within a given range
- _.isNative to check for native functions
- _.isPlainObject & _.toPlainObject to check for & convert to
Object
objects - _.isTypedArray to check for typed arrays
- _.matches supports deep object comparisons
- _.matchesProperty to complement _.matches & _.property
- _.merge for a deep _.extend
- _.parseInt for consistent cross-environment behavior
- _.pull, _.pullAt, & _.remove for mutating arrays
- _.random supports returning floating-point numbers
- _.restParam & _.spread for applying rest parameters & spreading arguments to functions
- _.runInContext for collisionless mixins & easier mocking
- _.slice for creating subsets of array-like values
- _.sortByAll & _.sortByOrder for sorting by multiple properties & orders
- _.sum to get the sum of values
- _.support for flagging environment features
- _.template supports “imports” options & ES template delimiters
- _.transform as a powerful alternative to _.reduce for transforming objects
- _.xor to complement _.difference, _.intersection, & _.union
- _.valuesIn for getting values of all enumerable properties
- _.bind, _.curry, _.partial, &
more support customizable argument placeholders
- _.capitalize, _.trim, &
more string methods
- _.clone, _.isEqual, &
more accept callbacks
- _.dropWhile, _.takeWhile, &
more to complement _.first, _.initial, _.last, & _.rest
- _.findLast, _.findLastKey, &
more right-associative methods
- _.includes, _.toArray, &
more accept strings
- _#commit & _#plant for working with chain sequences
- _#thru to pass values thru a chain sequence
Support
Tested in Chrome 40-41, Firefox 35-36, IE 6-11, Opera 27-28, Safari 5-8, io.js 1.6.2, Node.js 0.8.28, 0.10.36, & 0.12.0, PhantomJS 1.9.8, RingoJS 0.11, & Rhino 1.7RC5.
Automated browser & CI test runs are available. Special thanks to Sauce Labs for providing automated browser testing.