6/15/2018 Lodash cheatsheet
DEVHINTS.IO Edit
Lodash cheatsheet
Finding Accessing
_.filter(list, (n) => n % 2) // → Array _.at([ abcd ], 0) // → [ a ] - same as
_.find(list, (n) => n % 2) // → item _.at([ abcd ], [ 0, 1 ]) // → [ ab ]
_.findRight(list, ...) // → item
Works for both arrays and objects. Set/get
_.set(object, 'users[0].name', value)
Iteration _.get(object, 'users[0].name')
_.get(object, ['users', 0, 'name'])
_.forEach(list, (item, i) => ...)
_.forEachRight(list, ...)
_.map(list, ...)
_.every(users, (u) => u.active) // → true|false (aka _.all)
_.any(users, ...) // → true|false (aka _.some)
https://devhints.io/lodash 1/7
6/15/2018 Lodash cheatsheet
Array
Arrays Filtering
_.chunk([ abcd ], 2) // → [ [ab], [cd] ] _.drop([ abcdef ], 2) // → [ cdef ]
_.compact(list) _.dropRight([ abcdef ], 2) // → [ abcd ]
_.take([ abcdef ], 2) // → [ ab ]
_.fill(Array(4), 'x') // → [ 'x', 'x', 'x', 'x' ] _.takeRight([ abcdef ], 2) // → [ de ]
_.flatten _.slice([ abcdef ], 2, 4) // → [ cd ]
_.flattenDeep
_.initial([ abcdef ]) // → [ abcde ] - dro
_.rest([ abcdef ]) // → [ bcdef ] - tak
Accessing
_.first([ abcdef ]) // → a _.dropWhile(list, 'active') // works like
_.last([ abcdef ]) // → f _.dropWhile(list, 'active', true)
_.dropWhile(list, { active: true })
_.dropWhile(list, (n) => ...)
Sets _.dropRightWhile(list, ...)
_.uniq()
_.without([ abcde ], b) // → [ acde ]
_.difference([ abc ], [ bc ]) // → [ a ]
_.intersection([ abc ], [ bcd ]) // → [ bc ]
_.union([ abc ], [ bcd ]) // → [ abcd ] (unique) _.remove(list, (n) => n % 2)
Indexes
Array#concat()
https://devhints.io/lodash 2/7
6/15/2018 Lodash cheatsheet
_.findIndex(list, fn)
_.findLastIndex(list, fn)
_.sortedIndex(list, val)
_.sortedLastIndex(list, val)
_.indexOf(list, val)
Functions
Currying
greet = (greeting, name) => `${greeting}, ${name}!`
fn = _.partial(fn, 'hi')
fn('joe') // → 'hi, joe!'
fn = _.partial(fn, 'joe')
fn('yo') // → 'yo, joe!'
_.curry(greet)('hi') // → function(name)
_.curryRight(greet)('joe') // → function(greet)
https://devhints.io/lodash 3/7
6/15/2018 Lodash cheatsheet
Decorating functions
Throttling Limiting
_.throttle(fn) _.before(5, fn) // only works 5 times
_.debounce(fn) _.after(5, fn) // works only after 5 times
_.once(fn) // like _.before(fn, 1)
Etc
_.wrap(_.escape, (name) => `hi ${name}`)
// same as doing `name = _.escape(name)`
_.delay(fn, 2000)
_.negate(fn)
_.memoize(fn)
_.memoize(fn, ...)
Strings
Capitalization Padding
_.capitalize('hello world') // → 'Hello world' _.pad('abc', 8) // → ' abc '
_.startCase('hello_world') // → 'Hello World' _.padLeft('abc', 8) // → ' abc'
https://devhints.io/lodash 4/7
6/15/2018 Lodash cheatsheet
_.snakeCase('hello world') // → 'hello_world' _.padLeft('abc', 8, '-') // → '00000abc'
_.kebabCase('hello world') // → 'hello-world' _.padRight(...)
_.camelCase('hello world') // → 'helloWorld'
Trim
Etc
_.trim(' str ') // → 'str'
_.repeat('-', 2) // → '--' _.trimLeft(' str ') // → 'str '
_.deburr('déjà vu') // → 'deja vu' _.trimRight(' str ') // → ' str'
_.trunc('hello world', 5) // → 'hello...'
_.startsWith('abc', 'a') // → true
_.endsWith('abc', 'c') // → true
Objects
Keys and values
_.keys(obj)
_.values(obj)
Chaining
https://devhints.io/lodash 5/7
6/15/2018 Lodash cheatsheet
Chain and value
_([1, 2, 3])
.reduce((total, n) => total + n)
.map((n) => n * n)
.tap(console.log)
.thru((n) => n.reverse())
.value()
devhints.io / Search 368+ cheatsheets
Other JavaScript libraries cheatsheets Top cheatsheets
Chai.js Flow Elixir ES2015+
cheatsheet cheatsheet cheatsheet cheatsheet
Over 368 curated
cheatsheets, by Jest Pug React.js Vim
developers for developers. cheatsheet cheatsheet cheatsheet cheatsheet
Devhints home
Yarn bluebird.js Vim scripting Capybara
https://devhints.io/lodash 6/7
6/15/2018 Lodash cheatsheet
cheatsheet cheatsheet cheatsheet cheatsheet
https://devhints.io/lodash 7/7