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 v0.10.0
A utility library delivering consistency, customization, performance, & extras.
Download
Dive in
We’ve got API docs, benchmarks, and unit tests.
Create your own benchmarks at jsPerf, or search for existing ones.
For a list of upcoming features, check out our roadmap.
Screencasts
For more information check out these screencasts over Lo-Dash:
Features
Support
Lo-Dash has been tested in at least Chrome 523, Firefox 116, IE 6-10, Opera 9.25-12, Safari 3-6, Node.js 0.4.8-0.8.14, Narwhal 0.3.2, RingoJS 0.8, 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');
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);
});
Resolved Underscore.js issues
- Allow iteration of objects with a
length
property [#799, test] - Fix cross-browser object iteration bugs [#60, #376, test]
- Methods should work on pages with incorrectly shimmed native methods [#7, #742, test]
_.isEmpty
should support jQuery/MooTools DOM query collections [#690, test]_.isObject
should avoid V8 bug #2291 [#605, test]_.keys
should work with arguments
objects cross-browser [#396, test]_.range
should coerce arguments to numbers [#634, #683, test]
Release Notes
v0.10.0
Compatibility Warnings
- Aligned
_.defaults
and _.extend
with ES6 Object.assign - Renamed
_.lateBind
to _.bindKey
Changes
- Added the build commands used to custom build copyright/license headers
- Added
_.assign
- Ensured the
underscore
build of _.find
returns the first, not last, matched value - Ensured
_defaults
, _.extends
, and _.merge
works with _.reduce
- Made Lo-Dash’s
npm
package installation work with more system configurations - Made
_.extend
an alias of _.assign
- Optimized
_.contains
, _.defaults
, _.extend
, and _.filter
- Restricted
_.where
to iterate only own properties of the source
object - Updated
backbone
build Lo-Dash method dependencies
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