0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

underscoreコードリーディング(contains)

Posted at

underscoreに詳しくないので、勉強半分でソースコードを読む。

利用するバージョン

underscore.js(v1.8.3)

containsとは

underscorejs.orgのcontains

こんな説明。

####_.contains(list, value, [fromIndex]) Alias: includes
Returns true if the value is present in the list.
Uses indexOf internally, if list is an Array.
Use fromIndex to start your search at a given index.

_.contains([1, 2, 3], 3);
=> true

_.includesにエイリアスが張られている

listの中に第二引数で指定したvalueが存在する場合trueを返す
listがArrayの場合は内部ではindexOfを利用します。
fromIndexを指定することによって開始位置を指定して検索することができます。

つまり

'use strict';

var _ = require( "underscore" );

console.log( _.contains( [ 1, 2, 3, 4, 5 ], 2 ) ); 
//-> true
console.log( _.contains( [ 1, 2, 3, 4, 5 ], 2, 3 ) );
//-> false

となる

underscore.contains

コード的にはこのあたり。


  // Determine if the array or object contains a given item (using `===`).
  // Aliased as `includes` and `include`.
  _.contains = _.includes = _.include = function(obj, item, fromIndex, guard) {
    if (!isArrayLike(obj)) obj = _.values(obj);
    if (typeof fromIndex != 'number' || guard) fromIndex = 0;
    return _.indexOf(obj, item, fromIndex) >= 0;
  };

aliasはinclude/includes両方に張られている(説明とはズレが有る)

objがArrayじゃなかった場合、obj = _.valuesとなる
fromIndexがnumberじゃなかった場合、またはguardが存在する場合、fromIndexは0となる
_.indexOfが0以上の場合true/そうでない場合はfalseが返される

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?