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.
Lo-Dash v1.3.1
A utility library delivering consistency, customization, performance, & extras.
Download
Dive in
We’ve got API docs, benchmarks, and unit tests.
For a list of upcoming features, check out our roadmap.
Features not in Underscore
- AMD loader support (RequireJS, curl.js, etc.)
- _(…) supports intuitive chaining
- _.at for cherry-picking collection values
- _.bindKey for binding “lazy” defined methods
- _.cloneDeep for deep cloning arrays and objects
- _.contains accepts a
fromIndex
argument - _.debounce and _.throttle accept an
options
argument for more control - _.createCallback to customize how callback arguments are handled and support callback shorthands in mixins
- _.findIndex and _.findKey for finding indexes and keys of collections
- _.forEach is chainable and supports exiting iteration early
- _.forIn for iterating over an object’s own and inherited properties
- _.forOwn for iterating over an object’s own properties
- _.isPlainObject checks if values are created by the
Object
constructor - _.merge for a deep _.extend
- _.parseInt for consistent cross-environment behavior
- _.partial and _.partialRight for partial application without
this
binding - _.runInContext for easier mocking and extended environment support
- _.support to flag environment features
- _.template supports “imports” options, ES6 template delimiters, and sourceURLs
- _.transform as a powerful alternative to _.reduce for transforming objects
- _.unzip as the inverse of _.zip
- _.where supports deep object comparisons
- _.clone, _.omit, _.pick,
and more… accept
callback
and thisArg
arguments - _.contains, _.size, _.toArray,
and more… accept strings
- _.filter, _.find, _.map,
and more… support “_.pluck” and “_.where”
callback
shorthands
Resources
For more information check out these articles, screencasts, and other videos over Lo-Dash:
Support
Lo-Dash has been tested in at least Chrome 527, Firefox 221, IE 6-10, Opera 9.25~12, Safari 3-6, Node.js 0.4.8-0.10.7 (Node bug #5622 prevents 0.10.8-0.10.10 from working), Narwhal 0.3.2, PhantomJS 1.9.0, RingoJS 0.9, and Rhino 1.7RC5.
Installation and usage
In browsers:
<script src="lodash.js"></script>
Using npm
:
npm install lodash
npm install -g lodash
npm link lodash
To avoid potential issues, update npm
before installing Lo-Dash:
npm install npm -g
In Node.js and RingoJS ≥ v0.8.0:
var _ = require('lodash');
var _ = require('lodash/dist/lodash.underscore');
Note: If Lo-Dash is installed globally, run npm link lodash
in your project’s root directory before requiring it.
In RingoJS ≤ v0.7.0:
var _ = require('lodash')._;
In Rhino:
load('lodash.js');
In an AMD loader like RequireJS:
require({
'paths': {
'underscore': 'path/to/lodash'
}
},
['underscore'], function(_) {
console.log(_.VERSION);
});
Release Notes
v1.3.1
- Added missing
cache
property to the objects returned by getObject
- Ensured
maxWait
unit tests pass in Ringo - Increased the
maxPoolSize
value - Optimized
releaseArray
and releaseObject
v1.3.0
- Added
_.transform
method - Added
_.chain
and _.findWhere
aliases - Added internal array and object pooling
- Added Istanbul test coverage reports to Travis CI
- Added
maxWait
option to _.debounce
- Added support for floating point numbers to
_.random
- Added Volo configuration to package.json
- Adjusted UMD for
component build
- Allowed more stable mixing of
lodash
and underscore
build methods - Ensured debounced function with,
leading
and trailing
options, works as expected - Ensured minified builds work with the Dojo builder
- Ensured minification avoids deoptimizing expressions containing boolean values
- Ensured unknown types return
false
in _.isObject
and _.isRegExp
- Ensured
_.clone
, _.flatten
, and _.uniq
can be used as a callback
for methods like _.map
- Ensured
_.forIn
works on objects with longer inheritance chains in IE < 9 - Ensured
_.isPlainObject
returns true
for empty objects in IE < 9 - Ensured
_.max
and _.min
chain correctly - Ensured
clearTimeout
use doesn’t cause errors in Titanium - Ensured that the
--stdout
build option doesn't write to a file - Exposed memoized function’s
cache
- Fixed
Error.prototype
iteration bugs - Fixed "scripts" paths in component.json
- Made methods support customizing
_.indexOf
- Made the build track dependencies of private functions
- Made the
template
pre-compiler build option avoid escaping non-ascii characters - Made
_.createCallback
avoid binding functions if they don’t reference this
- Optimized the Closure Compiler minification process
- Optimized the large array cache for
_.difference
, _.intersection
, and _.uniq
- Optimized internal
_.flatten
and _.indexOf
use - Reduced
_.unzip
and _.zip
- Removed special handling of arrays in
_.assign
and _.defaults
The full changelog is available here.
BestieJS
Lo-Dash is part of the BestieJS “Best in Class” module collection. This means we promote solid browser/environment support, ES5+ precedents, unit testing, and plenty of documentation.
Author
Contributors