Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Jordi Salvat i Alabart edited this pageJul 11, 2023 ·9 revisions

Underscore

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_.any is Lodash_.some
  • Underscore_.all is Lodash_.every
  • Underscore_.compose is Lodash_.flowRight
  • Underscore_.contains is Lodash_.includes
  • Underscore_.each doesn’t allow exiting by returningfalse is Lodash_.forEach
  • Underscore_.escape escapes backtick characters ('`'), while Lodash does not
  • Underscore_.findWhere is Lodash_.find
  • Underscore_.flatten is 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_.indexOf with 3rd parameterundefined is Lodash_.indexOf
  • Underscore_.indexOf with 3rd parametertrue is Lodash_.sortedIndexOf
  • Underscore_.indexBy is Lodash_.keyBy
  • Underscore_.invoke is Lodash_.invokeMap
  • Underscore_.mapObject is Lodash_.mapValues
  • Underscore_.max combines Lodash_.max &_.maxBy
  • Underscore_.min combines Lodash_.min &_.minBy
  • Underscore_.sample combines Lodash_.sample &_.sampleSize
  • Underscore_.object combines Lodash_.fromPairs and_.zipObject
  • Underscore_.omit by a predicate is Lodash_.omitBy
  • Underscore_.pairs is Lodash_.toPairs
  • Underscore_.pick by a predicate is Lodash_.pickBy
  • Underscore_.pluck is Lodash_.map
  • Underscore_.sortedIndex with an iteratee is Lodash_.sortedIndexBy, which doesn't take a 4th (context) parameter.
  • Underscore_.uniq by aniteratee is Lodash_.uniqBy._.uniq with theisSorted parameter = true is Lodash_.sortedUniq (or Lodash_.sortedUniqBy when using an iteree).
  • Underscore_.unique alias for_.uniq does not exist in Lodash
  • Underscore_.where is Lodash_.filter
  • Underscore_.isFinite doesn’t align withNumber.isFinite
    (e.g._.isFinite('1') returnstrue in Underscore butfalse in Lodash)
  • Underscore_.matches shorthand doesn’t support deep comparisons
    (e.g._.filter(objects, { 'a': { 'b': 'c' } }))
  • Underscore ≥ 1.7 & Lodash_.template syntax is
    _.template(string, option)(data)
  • Lodash_.memoize caches areMap like objects
  • Lodash doesn’t support acontext argument for many methods in favor of_.bind
  • Lodash supportsimplicit chaining,lazy chaining, & shortcut fusion
  • Lodash split its overloaded_.head,_.last,_.rest, &_.initial out into
    _.take,_.takeRight,_.drop, &_.dropRight
    (i.e._.head(array, 2) in Underscore is_.take(array, 2) in Lodash)

Backbone

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
module.exports={'resolve':{'alias':{'underscore':'absolute/path/to/lodash'}}};

Maintained by thecore team with help fromour contributors.

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp