- Notifications
You must be signed in to change notification settings - Fork7.1k
Migrating
Jordi Salvat i Alabart edited this pageJul 11, 2023 ·9 revisions
When migrating from Underscore to Lodash there are several differences to be aware of.
Below are some of the ones that stand out. For a more thorough guide check outlodash-codemods,eslint-plugin-lodash &Underscore to Lodash Converter.
- Underscore
_.anyis Lodash_.some - Underscore
_.allis Lodash_.every - Underscore
_.composeis Lodash_.flowRight - Underscore
_.containsis Lodash_.includes - Underscore
_.eachdoesn’t allow exiting by returningfalseis Lodash_.forEach - Underscore
_.escapeescapes backtick characters ('`'), while Lodash does not - Underscore
_.findWhereis Lodash_.find - Underscore
_.flattenis deep by default while Lodash is shallow - Underscore
_.groupBy's iteratee receives the argumentsvalue,indexNumber, andoriginalCollection, while Lodash_.groupBy's iteratee receives only the argumentvalue - Underscore
_.indexOfwith 3rd parameterundefinedis Lodash_.indexOf - Underscore
_.indexOfwith 3rd parametertrueis Lodash_.sortedIndexOf - Underscore
_.indexByis Lodash_.keyBy - Underscore
_.invokeis Lodash_.invokeMap - Underscore
_.mapObjectis Lodash_.mapValues - Underscore
_.maxcombines Lodash_.max&_.maxBy - Underscore
_.mincombines Lodash_.min&_.minBy - Underscore
_.samplecombines Lodash_.sample&_.sampleSize - Underscore
_.objectcombines Lodash_.fromPairsand_.zipObject - Underscore
_.omitby a predicate is Lodash_.omitBy - Underscore
_.pairsis Lodash_.toPairs - Underscore
_.pickby a predicate is Lodash_.pickBy - Underscore
_.pluckis Lodash_.map - Underscore
_.sortedIndexwith an iteratee is Lodash_.sortedIndexBy, which doesn't take a 4th (context) parameter. - Underscore
_.uniqby aniterateeis Lodash_.uniqBy._.uniqwith theisSortedparameter = true is Lodash_.sortedUniq(or Lodash_.sortedUniqBywhen using an iteree). - Underscore
_.uniquealias for_.uniqdoes not exist in Lodash - Underscore
_.whereis Lodash_.filter - Underscore
_.isFinitedoesn’t align withNumber.isFinite
(e.g._.isFinite('1')returnstruein Underscore butfalsein Lodash) - Underscore
_.matchesshorthand doesn’t support deep comparisons
(e.g._.filter(objects, { 'a': { 'b': 'c' } })) - Underscore ≥ 1.7 & Lodash
_.templatesyntax is_.template(string, option)(data) - Lodash
_.memoizecaches areMaplike objects - Lodash doesn’t support a
contextargument for many methods in favor of_.bind - Lodash supportsimplicit chaining,lazy chaining, & shortcut fusion
- Lodash split its overloaded
_.head,_.last,_.rest, &_.initialout into_.take,_.takeRight,_.drop, &_.dropRight
(i.e._.head(array, 2)in Underscore is_.take(array, 2)in Lodash)
Lodash works great with Backbone. It’s even run against Backone’s unit tests on every commit.
You can replace Underscore with Lodash in Backbone by using anAMD loader,browserify, orwebpack.
Note: Lodash v4 works with Backbone ≥ v1.3.0.
- Using AMD “paths” configuration
require({'paths'{'backbone':'path/to/backbone','jquery':'path/to/jquery','underscore':'path/to/lodash'}},['backbone'],function(Backbone){// use Backbone});
- Using thealiasify transform for browserify by adding a section to your package.json
{"browserify": {"transform": ["aliasify"] },"aliasify": {"aliases": {"underscore":"lodash" } }}and executing browserify with thealiasify parameter
$ browserify entry.js --global-transform aliasify -o out.js
- Using theresolve.alias configuration in webpack
module.exports={'resolve':{'alias':{'underscore':'absolute/path/to/lodash'}}};
Maintained by thecore team with help fromour contributors.