Lodash - Quick Guide

Lodash is a popular javascript based library which provides 200+ functions to facilitate web development. It provides helper functions like map, filter, invoke as well as function binding, javascript templating, deep equality checks, creating indexes and so on. Lodash can be used directly inside a browser and also with Node.js.
Working with objects using JavaScript can be quite challenging, specifically if you have lots of manipulation to be done with them. Lodash comes with lots of features that eases your work with objects.
Lodash is an open source project and you can easily contribute to the library and add features in the form of plugins and make it available on GitHub and in Node.js.
Let us understand in detail all the important features available with Lodash −
Lodash provides various functions for collections like each, map, reduce which are used to apply an operation on each item of a collection. It provides method like groupBy, countBy, max, min which processes collections and ease lot of tasks.
Lodash provides various functions for arrays like to iterate and process arrays like first, initial, lastIndexOf, intersection, difference etc.
Lodash provides functions such as bind, delay, before, after etc.
Lodash provides functions to manipulate objects, to map objects and comparing objects. For example, keys, values, extends, extendsOwn, isEqual, isEmpty etc.
Lodash provides various utilities methods like noConflict, random, iteratee, escape etc.
Lodash provides chaining methods as well like chain, value.
In subsequent chapters, we'll cover important functions of Lodash
In this chapter, you will learn in detail about setting up the working environment of Lodash on your local computer. Before you begin with working on Lodash, you need to have the access to the library. You can access its files in any of the following methods −
In this method, we are going to need Lodash file from its official website and will use it directly in the browser.
As a first step, go to the official website of Lodash https://lodash.com/.
Observe that there is a download option available which gives you the latest lodash.min.js file CDN Copies available. Click on the link and select latest link for lodash.min.js.
Now, include lodash.min.js inside the script tag and start working with Lodash. For this, you can use the code given below −
<script type = "text/JavaScript" 
   src = "https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js">
</script>
Given here is a working example and its output for a better understanding −
<html>
   <head>
      <title>Lodash - Working Example</title>
      <script type = "text/JavaScript" src = "https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
      <style>
         div {
            border: solid 1px #ccc;
            padding:10px;
            font-family: "Segoe UI",Arial,sans-serif;
            width: 50%;
         }
      </style>
   </head>
   <body>
      <div style = "font-size:25px" id = "list"></div>
      <script type = "text/JavaScript">
         var numbers = [1, 2, 3, 4];
         var listOfNumbers = '';
         _.each(numbers, function(x) { listOfNumbers += x + ' ' });
         document.getElementById("list").innerHTML = listOfNumbers;
      </script>
   </body>
</html>
If you are opting for this method, make sure you have Node.js and npm installed on your system. You can use the following command to install Lodash −
npm install lodash

You can observe the following output once Lodash is successfully installed −
+ lodash@4.17.20
added 1 package from 2 contributors and audited 1 package in 2.54s
found 0 vulnerabilities

Now, to test if Lodash works fine with Node.js, create the file tester.js and add the following code to it −
var _ = require('lodash');
var numbers = [1, 2, 3, 4];
var listOfNumbers = '';
_.each(numbers, function(x) { listOfNumbers += x + ' ' });
console.log(listOfNumbers);
Save the above program in tester.js. The following commands are used to compile and execute this program.
\>node tester.js

1 2 3 4

Lodash has many easy to use methods which helps in processing Arrays. This chapter discusses them in detail.
Lodash provides various methods to process the Arrays as listed below −
chunk
_.chunk(array, [size=1])
compact
_.compact(array)
concat
_.concat(array, [values])
difference
_.difference(array, [values])
differenceBy
_.differenceBy(array, [values], [iteratee=_.identity])
differenceWith
_.differenceWith(array, [values], [comparator])
drop
_.drop(array, [n=1])
dropRight
_.dropRight(array, [n=1])
dropRightWhile
_.dropRightWhile(array, [predicate=_.identity])
dropWhile
_.dropWhile(array, [predicate=_.identity])
fill
_.fill(array, value, [start=0], [end=array.length])
findIndex
_.findIndex(array, [predicate=_.identity], [fromIndex=0])
findLastIndex
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])
flatten
_.flatten(array)
flattenDeep
_.flattenDeep(array)
flattenDepth
_.flattenDepth(array, [depth=1])
fromPairs
_.fromPairs(pairs)
head
_.head(array)
indexOf
_.indexOf(array, value, [fromIndex=0])
initial
_.initial(array)
intersection
_.intersection([arrays])
intersectionBy
_.intersectionBy([arrays], [iteratee=_.identity])
intersectionWith
_.intersectionWith([arrays], [comparator])
join
_.join(array, [separator=','])
last
_.last(array)
lastIndexOf
_.lastIndexOf(array, value, [fromIndex=array.length-1])
nth
_.nth(array, [n=0])
pull
_.pull(array, [values])
pullAll
_.pullAll(array, values)
pullAllBy
_.pullAllBy(array, values, [iteratee=_.identity])
pullAllWith
_.pullAllWith(array, values, [comparator])
pullAt
_.pullAt(array, [indexes])
remove
_.remove(array, [predicate=_.identity])
reverse
_.reverse(array)
slice
_.slice(array, [start=0], [end=array.length])
sortedIndex
_.sortedIndex(array, value)
sortedIndexBy
_.sortedIndexBy(array, value, [iteratee=_.identity])
sortedIndexOf
_.sortedIndexOf(array, value)
sortedLastIndex
_.sortedLastIndex(array, value)
sortedLastIndexBy
_.sortedLastIndexBy(array, value, [iteratee=_.identity])
sortedLastIndexOf
_.sortedLastIndexOf(array, value)
sortedUniq
_.sortedUniq(array)
sortedUniqBy
_.sortedUniqBy(array, [iteratee])
tail
_.tail(array)
take
_.take(array, [n=1])
takeRight
_.takeRight(array, [n=1])
takeRightWhile
_.takeRightWhile(array, [predicate=_.identity])
takeWhile
_.takeWhile(array, [predicate=_.identity])
union
_.union([arrays])
unionBy
_.unionBy([arrays], [iteratee=_.identity])
unionWith
_.unionWith([arrays], [comparator])
uniq
_.uniq(array)
uniqBy
_.uniqBy(array, [iteratee=_.identity])
uniqWith
_.uniqWith(array, [comparator])
unzip
_.unzip(array)
unzipwith
_.unzipWith(array, [iteratee=_.identity])
without
_.without(array, [values])
xor
_.xor([arrays])
xorBy
_.xorBy([arrays], [iteratee=_.identity])
xorWith
_.xorWith([arrays], [comparator])
zip
_.zip([arrays])
zipObject
_.zipObject([props=[]], [values=[]])
zipObjectDeep
_.zipObjectDeep([props=[]], [values=[]])
zipWith
_.zipWith([arrays], [iteratee=_.identity])
Lodash has many easy to use methods which helps in processing Collections. This chapter discusses them in detail.
Lodash provides various methods to process the Collections as listed below −
countBy
_.countBy(collection, [iteratee=_.identity])
every
_.every(collection, [predicate=_.identity])
filter
_.filter(collection, [predicate=_.identity])
find
_.find(collection, [predicate=_.identity], [fromIndex=0])
findLast
_.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1])
flatMap
_.flatMap(collection, [iteratee=_.identity])
flatMapDeep
_.flatMapDeep(collection, [iteratee=_.identity])
flatMapDepth
_.flatMapDepth(collection, [iteratee=_.identity], [depth=1])
forEach
_.forEach(collection, [iteratee=_.identity])
forEachRight
_.forEachRight(collection, [iteratee=_.identity])
groupBy
_.groupBy(collection, [iteratee=_.identity])
includes
_.includes(collection, value, [fromIndex=0])
invokeMap
_.invokeMap(collection, path, [args])
keyBy
_.keyBy(collection, [iteratee=_.identity])
map
_.map(collection, [iteratee=_.identity])
orderBy
_.orderBy(collection, [iteratees=[_.identity]], [orders])
partition
_.partition(collection, [predicate=_.identity])
reduce
_.reduce(collection, [iteratee=_.identity], [accumulator])
reduceRight
_.reduceRight(collection, [iteratee=_.identity], [accumulator])
reject
_.reject(collection, [predicate=_.identity])
sample
_.sample(collection)
sampleSize
_.sampleSize(collection, [n=1])
shuffle
_.shuffle(collection)
size
_.size(collection)
some
_.some(collection, [predicate=_.identity])
sortBy
_.sortBy(collection, [iteratees=[_.identity]])
Lodash provides a now function to get the current time in milliseconds.
_.now()

Gets the timestamp of the number of milliseconds that have elapsed since the Unix epoch (1 January 1970 00:00:00 UTC).
(number) − Returns the timestamp.
(number) − Returns the timestamp.
var _ = require('lodash');
var result = _.now();
console.log(result);
Save the above program in tester.js. Run the following command to execute this program.
\>node tester.js

1601614929848

Lodash has many easy to use methods which helps in creating and processing Functions. This chapter discusses them in detail.
Lodash provides various methods to process the Functions as listed below −
ary
_.ary(func, [n=func.length])
before
_.before(n, func)
bind
_.bind(func, thisArg, [partials])
bindKey
_.bindKey(object, key, [partials])
curry
_.curry(func, [arity=func.length])
curryRight
_.curryRight(func, [arity=func.length])
delay
_.delay(func, wait, [args])
flip
_.flip(func)
memoize
_.memoize(func, [resolver])
negate
_.negate(predicate)
once
_.once(func)
overArgs
_.overArgs(func, [transforms=[_.identity]])
partial
_.partial(func, [partials])
partialRight
_.partialRight(func, [partials])
rearg
_.rearg(func, indexes)
rest
_.rest(func, [start=func.length-1])
spread
_.spread(func, [start=0])
unary
_.unary(func)
wrap
_.wrap(value, [wrapper=identity])
Lodash has many easy to use general purpose methods. This chapter discusses them in detail.
Lodash provides various general purpose methods as listed below −
castArray
_.castArray(value)
clone
_.clone(value)
cloneDeep
_.cloneDeep(value)
conformsTo
_.conformsTo(object, source)
eq
_.eq(value, other)
gt
_.gt(value, other)
gte
_.gte(value, other)
isArguments
_.isArguments(value)
isArray
_.isArray(value)
isArrayBuffer
_.isArrayBuffer(value)
isArrayLike
_.isArrayLike(value)
isArrayLikeObject
_.isArrayLikeObject(value)
isBoolean
_.isBoolean(value)
isBuffer
_.isBuffer(value)
isDate
_.isDate(value)
isEmpty
_.isEmpty(value)
isEqual
_.isEqual(value, other)
isEqualWith
_.isEqualWith(value, other, [customizer])
isError
_.isError(value)
isFinite
_.isFinite(value)
isFunction
_.isFunction(value)
isInteger
_.isInteger(value)
isLength
_.isLength(value)
isMap
_.isMap(value)
isMatch
_.isMatch(object, source)
isMatchWith
_.isMatchWith(object, source, [customizer])
isNaN
_.isNaN(value)
isNative
_.isNative(value)
isNil
_.isNil(value)
isNull
_.isNull(value)
isNumber
_.isNumber(value)
isObject
_.isObject(value)
isObjectLike
_.isObjectLike(value)
isPlainObject
_.isPlainObject(value)
isRegExp
_.isRegExp(value)
isSafeInteger
_.isSafeInteger(value)
isSet
_.isSet(value)
isString
_.isString(value)
isSymbol
_.isSymbol(value)
isTypedArray
_.isTypedArray(value)
isUndefined
_.isUndefined(value)
isWeakMap
_.isWeakMap(value)
isWeakSet
_.isWeakSet(value)
lt
_.lt(value, other)
lte
_.lte(value, other)
toArray
_.toArray(value)
toFinite
_.toFinite(value)
toInteger
_.toInteger(value)
toLength
_.toLength(value)
toNumber
_.toNumber(value)
toPlainObject
_.toPlainObject(value)
toSafeInteger
_.toSafeInteger(value)
toString
_.toString(value)
Lodash has many easy to use Math related methods. This chapter discusses them in detail.
Lodash provides various Math related methods as listed below −
add
_.add(augend, addend)
ceil
_.ceil(number, [precision=0])
divide
_.divide(dividend, divisor)
floor

_.floor(number, [precision=0])
max
_.max(array)
maxBy
_.maxBy(array, [iteratee=_.identity])
mean
_.mean(array)
meanBy
_.meanBy(array, [iteratee=_.identity])
min
_.min(array)
minBy
_.minBy(array, [iteratee=_.identity])
multiply
_.multiply(multiplier, multiplicand)
round
_.round(number, [precision=0])
subtract
_.subtract(minuend, subtrahend)
sum
_.sum(array)
sumBy
_.sumBy(array, [iteratee=_.identity])
Lodash has many easy to use Number related methods. This chapter discusses them in detail.
Lodash provides various Number related methods as listed below −
clamp
_.clamp(number, [lower], upper)
inRange
_.inRange(number, [start=0], end)
random
_.random([lower=0], [upper=1], [floating])
Lodash has many easy to use Object related methods. This chapter discusses them in detail.
Lodash provides various Object related methods as listed below −
assign
_.assign(object, [sources])
assignIn
_.assignIn(object, [sources])
assignInWith
_.assignInWith(object, sources, [customizer])
assignWith
_.assignWith(object, sources, [customizer])
at
_.at(object, [paths])
create
_.create(prototype, [properties])
defaults
_.defaults(object, [sources])
defaultsDeep
_.defaultsDeep(object, [sources])
findKey
_.findKey(object, [predicate=_.identity])
findLastKey
_.findLastKey(object, [predicate=_.identity])
forIn
_.forIn(object, [iteratee=_.identity])
forInRight
_.forInRight(object, [iteratee=_.identity])
forOwn
_.forOwn(object, [iteratee=_.identity])
forOwnRight
_.forOwnRight(object, [iteratee=_.identity])
functions
_.functions(object)
functionsIn
_.functionsIn(object)
get
_.get(object, path, [defaultValue])
has
_.has(object, path)
hasIn
_.hasIn(object, path)
invert
_.invert(object)
invertBy
_.invertBy(object, [iteratee=_.identity])
invoke
_.invoke(object, path, [args])
keys
_.keys(object)
keysIn
_.keysIn(object)
mapKeys
_.mapKeys(object, [iteratee=_.identity])
mapValues
_.mapValues(object, [iteratee=_.identity])
merge
_.merge(object, [sources])
mergeWith
_.mergeWith(object, sources, customizer)
omit
_.omit(object, [paths])
omitBy
_.omitBy(object, [predicate=_.identity])
pick
_.pick(object, [paths])
pickBy
_.pickBy(object, [predicate=_.identity])
result
_.result(object, path, [defaultValue])
set
_.set(object, path, value)
setWith
_.setWith(object, path, value, [customizer])
toPairs
_.toPairs(object)
toPairsIn
_.toPairsIn(object)
transform
_.transform(object, [iteratee=_.identity], [accumulator])
unset
_.unset(object, path)
update
_.update(object, path, updater)
updateWith
_.updateWith(object, path, updater, [customizer])
values
_.values(object)
valuesIn
_.valuesIn(object)
Lodash has many easy to use Sequence related methods. This chapter discusses them in detail.
Lodash provides various Sequence related methods as listed below −
chain
_.chain(value)
tap
_.tap(value, interceptor)
thru
_.thru(value, interceptor)
prototype[Symbol.iterator]
_.prototype[Symbol.iterator]()
prototype.at
_.prototype.at([paths])
prototype.chain
_.prototype.chain()
prototype.commit
_.prototype.commit()
prototype.next
_.prototype.next()
prototype.plant
_.prototype.plant(value)
prototype.reverse
_.prototype.reverse()
prototype.value
_.prototype.value()
Lodash has many easy to use String manipulation methods. This chapter discusses them in detail.
Lodash provides various String related methods as listed below −
camelCase
_.camelCase([string=''])
capitalize
_.capitalize([string=''])
deburr
_.deburr([string=''])
endsWith
_.endsWith([string=''], [target], [position=string.length])
escape
_.escape([string=''])
escapeRegExp
_.escapeRegExp([string=''])
kebabCase
_.kebabCase([string=''])
lowerCase
_.lowerCase([string=''])
lowerFirst
_.lowerFirst([string=''])
pad
_.pad([string=''], [length=0], [chars=' '])
padEnd
_.padEnd([string=''], [length=0], [chars=' '])
padStart
_.padStart([string=''], [length=0], [chars=' '])
parseInt
_.parseInt(string, [radix=10])
repeat
_.repeat([string=''], [n=1])
replace
_.replace([string=''], pattern, replacement)
snakeCase
_.snakeCase([string=''])
spilt
_.split([string=''], separator, [limit])
startCase
_.startCase([string=''])
startsWith
_.startsWith([string=''], [target], [position=0])
template
_.template([string=''], [options={}])
toLower
_.toLower([string=''])
toUpper
_.toUpper([string=''])
trim
_.trim([string=''], [chars=whitespace])
trimEnd
_.trimEnd([string=''], [chars=whitespace])
trimStart
_.trimStart([string=''], [chars=whitespace])
truncate
_.truncate([string=''], [options={}])
unescape
_.unescape([string=''])
upperCase
_.upperCase([string=''])
upperFirst
_.upperFirst([string=''])
words
_.words([string=''], [pattern])
Lodash has many easy to use Utility methods. This chapter discusses them in detail.
Lodash provides various Utility methods as listed below −
cond
_.cond(pairs)
conforms
_.conforms(source)
constant
_.constant(value)
defaultTo
_.defaultTo(value, defaultValue)
flow
_.flow([funcs])
flowRight
_.flowRight([funcs])
identity
_.identity(value)
iteratee
_.iteratee([func=_.identity])
matches
_.matches(source)
matchesProperty
_.matchesProperty(path, srcValue)
method
_.method(path, [args])
methodOf
_.methodOf(object, [args])
mixin
_.mixin([object=lodash], source, [options={}])
noConflict
_.noConflict()
noop
_.noop()
nthArg
_.nthArg([n=0])
over
_.over([iteratees=[_.identity]])
overEvery
_.overEvery([predicates=[_.identity]])
overSome
_.overSome([predicates=[_.identity]])
property
_.property(path)
propertyOf
_.propertyOf(object)
range
_.range([start=0], end, [step=1])
rangeRight
_.rangeRight([start=0], end, [step=1])
runInContext
_.runInContext([context=root])
stubArray
_.stubArray()
stubFalse
_.stubFalse()
stubObject
_.stubObject()
stubString
_.stubString()
stubTrue
_.stubTrue()
times
_.times(n, [iteratee=_.identity])
toPath
_.toPath(value)
uniqueId
_.uniqueId([prefix=''])
This chapter discusses lodash properties in detail.
_.VERSION − (string): The semantic version number.
_.templateSettings − (Object): By default, the template delimiters used by lodash are like those in embedded Ruby (ERB) as well as ES2015 template strings. Change the following template settings to use alternative delimiters.
_.templateSettings.escape − (RegExp): Used to detect data property values to be HTML-escaped.
_.templateSettings.evaluate − (RegExp): Used to detect code to be evaluated.
_.templateSettings.imports − (Object): Used to import variables into the compiled template.
_.templateSettings.interpolate − (RegExp): Used to detect data property values to inject.
_.templateSettings.variable − (string): Used to reference the data object in the template text.
This chapter discusses lodash properties in detail.
_.VERSION − (string): The semantic version number.
_.templateSettings − (Object): By default, the template delimiters used by lodash are like those in embedded Ruby (ERB) as well as ES2015 template strings. Change the following template settings to use alternative delimiters.
_.templateSettings.escape − (RegExp): Used to detect data property values to be HTML-escaped.
_.templateSettings.evaluate − (RegExp): Used to detect code to be evaluated.
_.templateSettings.imports − (Object): Used to import variables into the compiled template.
_.templateSettings.interpolate − (RegExp): Used to detect data property values to inject.
_.templateSettings.variable − (string): Used to reference the data object in the template text.
 Print
 Add Notes
 Bookmark this page